簡體   English   中英

角度搜索過濾器

[英]Angular search filter

我正在嘗試在角度2中實現搜索過濾器,因為我已經實現了以下代碼

app.module.ts

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import{Pipe,PipeTransform} from '@angular/core';
import { AppComponent } from './app.component';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';

@NgModule({
  declarations: [
    AppComponent,   
  ],
  imports: [
    BrowserModule,
    FormsModule,
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

app.component.ts

import { Component, Injectable } from '@angular/core';
import {Pipe, PipeTransform} from '@angular/core';

@Pipe({
    name: 'searchFilter'
})
export class SearchFilter implements PipeTransform {
    transform(items: any[], criteria: any): any {

        return items.filter(item =>{
           for (let key in item ) {
             if((""+item[key]).toLocaleLowerCase().includes(criteria.toLocaleLowerCase())){
                return true;
             }
           }
           return false;
        });
    }
}

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent implements P {
  title = 'app';
    users:any[]=[
           {sid:124,sname:"Angular"},
           {sid:125,sname:"Ionic"},
           {sid:126,sname:"mobile"}]
  }

app.component.html

<div class="container">
<h4>Creating Custom Pipes</h4>
 <input #search  (keyup)="0">
 <div *ngFor="let user of (users | searchFilter: search.value )">
   {{user.sname}}
  </div>  
</div>

但是在瀏覽器控制台日志中,它顯示以下錯誤

未捕獲的錯誤:模板解析錯誤:找不到管道'searchFilter'(“。

我無法弄清楚是什么錯誤。 任何人都可以幫助實現這一目標

在聲明下的模塊中添加SearchFilter

NgModule({
  declarations: [
    AppComponent,  
    SearchFilter  
  ],
  imports: [
    BrowserModule,
    FormsModule,
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM