我想创建一个动态管道,我将从组件中调用它.
import {Component,Pipe,PipeTransform} from 'angular2/core'; @Pipe({ name: 'filter',pure: false }) export class filter implements PipeTransform { transform(value) { this.items1=value; this.ticket1 = []; if (this.items1.length >0) { for (var i = 0; i < this.items1.length; i++) { this.ticket1.push(this.items1[i]) } } } }
我想从组件中调用此管道.
您需要在组件的pipes属性中指定它
@Component({ pipes: [ filter ] }) export class MyComponent { (...) }
并在您的模板中使用它:
{{someArray | filter}} <div *ngFor="someArray | filter">(...)</div>
编辑
如果要直接在组件类中调用管道,则需要实例化它并调用其tranform方法:
@Component({ (...) }) export class MyComponent { constructor() { let filterPipe = new filter(); let arr = [ ... ]; var fiteredArr = filterPipe.transform(arr); } (...) }