我做了一个PIPE如下:
import {Pipe, PipeTransform} from '@angular/core';
import { Radio } from '../../models/radio';
@Pipe({
name: 'radioFilter'
})
export class radioFilterPipe implements PipeTransform {
transform(value: Radio[], args: string[]): any {
let filter = args[0].toLocaleLowerCase();
return filter ? value.filter(radio => radio.station.text.toLocaleLowerCase().indexOf(filter) != -1) : value;
}
}
在我的组件中添加了以下代码:
import { radioFilterPipe } from './grid.station.pipe';
pipes: [radioFilter],
但是我收到编译错误:错误TS2304:找不到名称' radioFilter'。我做错了什么?
答案 0 :(得分:9)
根据Angular 2最终版本,您必须将Pipe
,Component
,Directive
添加到declarations
元数据的NgModule
选项中。
@NgModule({
declarations: [MyDirective, MyComponent, radioFilterPipe ],
providers: [radioFilterPipe], //<-- Include pipe here if you want to use it in class as dependency
imports: [ AppModule]
})
在pipe
数组中包含pipes
名称时,它应该是类名radioFilterPipe
import { radioFilterPipe } from './grid.station.pipe';
@Component({
..,
pipes: [radioFilterPipe ],
..
})