【问题标题】:Angular search filter角度搜索过滤器
【发布时间】:2018-06-23 18:43:13
【问题描述】:

我正在尝试在 angular 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
  • 下次再​​说:如果你使用ng generate pipe SearchFilter 创建管道,angular-cli 将创建文件并将正确的模块注入到你的 app.module.ts 中

标签: angular filter pipe


【解决方案1】:

在你的模块中的声明下添加SearchFilter

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-31
    • 1970-01-01
    • 1970-01-01
    • 2022-11-27
    • 2014-11-29
    • 2014-09-01
    相关资源
    最近更新 更多