How to create and call a pipe from the component in Angular 2?

后端 未结 5 766
臣服心动
臣服心动 2020-12-17 15:03

I want to create a dynamic pipe which I am going to call from the component.

import {Component, Pipe, PipeTransform} from \'angular2/core\';

@Pipe({ name: \         


        
5条回答
  •  情书的邮戳
    2020-12-17 15:55

    You need to register the pipes you want to use in a component:

    @Component({
      ...
      pipes: [filter],
      template: `
    
    {{item}}
    ` ...}) class SomeComponent { someData = [ ... ]; }
    @NgModule({
      imports: [CommonModule],
      declarations: [filter]
    })
    export class MyFilterModule()
    

    To make the pipe available add the module to imports where you want to use it

    @NgModule({
      declarations: [AppComponent, SomeComponent],
      imports: [BrowserModule, MyFilterModule]
    })
    export class AppModuleModule()
    

    If you want to call the pipe from code

    let f = new filter();
    f.transform(value, filterArg);
    

提交回复
热议问题