Angular 2 创建自定义管道
本文向大家介绍Angular 2 创建自定义管道,包括了Angular 2 创建自定义管道的使用技巧和注意事项,需要的朋友参考一下
示例
app / pipes.pipe.ts
import { Pipe, PipeTransform } from '@angular/core'; @Pipe({name: 'truthy'}) export class Truthy implements PipeTransform { transform(value: any, truthy: string, falsey: string): any { if (typeof value === 'boolean'){return value ? truthy : falsey;} else return value } }
app / my-component.component.ts
import { Truthy} from './pipes.pipe'; @Component({ selector: 'my-component', template: ` <p>{{value | truthy:'enabled':'disabled' }}</p> `, pipes: [Truthy] }) export class MyComponent{ }