Angular2 Pipe无法找到名称

时间:2016-06-29 16:12:53

标签: angular angular-pipe

我做了一个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'。我做错了什么?

1 个答案:

答案 0 :(得分:9)

根据Angular 2最终版本,您必须将PipeComponentDirective添加到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 ],
  ..
})