6

我做了一个管道如下:

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”。我究竟做错了什么??

4

1 回答 1

13

根据 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 ],
  ..
})
于 2016-06-29T16:15:17.697 回答