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

后端 未结 5 767
臣服心动
臣服心动 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条回答
  •  旧时难觅i
    2020-12-17 15:42

    In case you want to use your pipe on different modules several times, I suggest you aggregate all of your pipes into one module (e.g., PipesAggrModule) and then import this module into the wanted module. For instance:

    my-pipe.module.ts

    @Pipe({name: 'MyPipe'})
    export class MyPipe { ... }
    

    pipes-aggr.module.ts:

    @NgModule({
      imports: [
        CommonModule
      ],
      exports: [
        ...
        MyPipe,
        MyPipe2,
        ...
      ],
      declarations: [..., MyPipe, MyPipe2, ...]
    })
    export class PipesAggrModule {}
    

    Then, to use your pipes, simply import the import the PipesAggrModule into the wanted module.

    my.module.ts

    @NgModule({
      imports: [
        CommonModule,
        PipesAggrModule
      ],
    ...
    }
    export class MyModule {}
    

提交回复
热议问题