Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use a pipe in two different Angular Modules

I have a pipe

@Pipe({name: 'keys'})
export class KeysPipe implements PipeTransform {
  transform(value, args:string[]) : any {
  .....
    return keys;
  }
}

I have two modules in which I need to use this. If I do something like this in both the modules, I get an error saying that "two modules declare KeysPipe"

Module1, Module2:

declarations: [KeysPipe],

I then tried exporting KeysPipe through it's own module so that I can import it in to the two modules in which I need to use it

@NgModule({
    declarations: [ KeysPipe],
})
export class KeysPipeModule {
}

Now I'm importing the KeysPipeModule in the two modules I need to use KeysPipe

Module1, Module2:

imports: [KeysPipeModule],

But now I get a different template error saying that the pipe isn't found "The pipe 'keys' could not be found ("v *ngIf="docalc">"

like image 280
user6123723 Avatar asked Sep 04 '16 19:09

user6123723


People also ask

Can we use multiple pipes in Angular?

Angular Pipes Multiple custom pipesIt is possible to bundle all frequently used pipes in one Module and import that new module in any component needs the pipes.

Can Angular have multiple modules?

Yes, you can define multiple modules in angularJS as given below. The modularization in AngularJS helps us to keep the code clarity and easy to understand, as we can combine multiple modules to generate the application.


1 Answers

You're on the right track the only thing your code is missing is the export in the KeysPipeModule. This is what it should look like:

@NgModule({
    declarations: [ KeysPipe],
    exports: [KeysPipe]
})
export class KeysPipeModule {}
like image 186
Filip Lauc Avatar answered Sep 28 '22 05:09

Filip Lauc