我正在尝试在我已实现以下代码的角度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'(“。
我无法弄清楚是什么错误。谁能帮助实现这一目标
答案 0 :(得分:0)
在模块中的声明下添加 SearchFilter
NgModule({
declarations: [
AppComponent,
SearchFilter
],
imports: [
BrowserModule,
FormsModule,
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }