【问题标题】:Angular Ionic Firestore: Filter / SearchAngular Ionic Firestore:过滤/搜索
【发布时间】:2019-08-13 15:56:52
【问题描述】:

我希望根据页面顶部的输入过滤我的结果集。

主页标记

  <ion-searchbar [(ngModel)]="query"></ion-searchbar>

  <ion-card *ngFor="let note of noteList | search:'distillery':query | async" routerLink="/detail/{{note.id}}">

首页 TS

  import { Observable } from 'rxjs';
  import { Router } from '@angular/router'; 
  import { Note } from '../models/note.interface';
  import { FirestoreService } from '../services/data/firestore.service';
  import { Component, Pipe, PipeTransform } from '@angular/core';

  @Pipe({
    name: 'search'
  })
  export class SearchPipe implements PipeTransform {
    public transform(value, keys: string, term: string) {  
      if (!term) return value;
      return (value || []).filter(note => keys.split(',').some(key => note.hasOwnProperty(key) && new RegExp(term, 'gi').test(note[key])));

    }
  }

  @Component({
    selector: 'app-home',
    templateUrl: 'home.page.html',
    styleUrls: ['home.page.scss'],
  })
  export class HomePage {

    public noteList;
    constructor(
      private firestoreService: FirestoreService,
      private router: Router
    ) {}

    ngOnInit() {
      this.noteList = this.firestoreService.getNoteList().valueChanges();
    }
  }

我在控制台中遇到的错误是......

ERROR Error: Uncaught (in promise): Error: Template parse errors:
The pipe 'search' could not be found ("
  <ion-searchbar [(ngModel)]="query"></ion-searchbar>

  <ion-card *ngFor="let [ERROR ->]note of noteList | search:'distillery':query | async" routerLink="/detail/{{note.id}}">
    <ion-card"): ng:///HomePageModule/HomePage.html@15:24

【问题讨论】:

标签: angular ionic-framework google-cloud-firestore


【解决方案1】:

您需要将SearchPipe 注入尝试使用它的组件HomePage。您可以在组件的构造函数中执行此操作。

constructor(
  private firestoreService: FirestoreService,
  private router: Router,
  private search: SearchPipe
) {}

【讨论】:

    猜你喜欢
    • 2020-05-04
    • 1970-01-01
    • 2017-04-28
    • 1970-01-01
    • 2020-05-21
    • 2021-07-22
    • 2017-04-02
    • 2017-09-30
    • 1970-01-01
    相关资源
    最近更新 更多