I want to make a pipe available in all the app. Acording with what I read in the Angular docs and internet, if I declare a pipe into the root module declaratios, it make's the pipe available in all the application. I have this AppModule code:
@NgModule({
imports: [ BrowserModule, NavbarModule],
declarations: [ AppComponent, TranslatePipe],
bootstrap: [ AppComponent],
})
export class AppModule { }
And this for the child module:
@NgModule({
imports: [ CommonModule],
declarations: [ NavbarMenuComponent],//<---Call the pipe in this component
})
export class NavbarModule { }
The pipe:
@Pipe({
name: 'translate',
pure: false
})
export class TranslatePipe implements PipeTransform {
constructor() { }
transform(value: string, args: any[]): any {
return value + " translated";
}
}
But whe I call the pipe on the NavbarMenuComponent template it thows this error:
'The pipe "translate" could not be found'
If I declare the pipe in the child module declarations it works,but I need to make the pipe global, so when the app grows up, I don't need to declare this pipe(and other global pipes) in all modules. Is there a way to make the pipe global?
These filters are known as "Pipes" in Angular 2. Pipes allow us to change the data inside the template. Normally, a pipe takes the data and transforms this input to the desired output. There are many built-in pipes in Angular 2.
Use pipes to transform strings, currency amounts, dates, and other data for display. Pipes are simple functions to use in template expressions to accept an input value and return a transformed value. Pipes are useful because you can use them throughout your application, while only declaring each pipe once.
You need to add the module that contains the pipe (and has it in declarations: []
and exports: []
) to imports: [...]
of your current module, then it's available in the current module.
@NgModule({
imports: [ CommonModule],
declarations: [ TranslatePipe],
exports: [ TranslatePipe],
})
export class TranslateModule { }
@NgModule({
imports: [ CommonModule, TranslateModule],
declarations: [ NavbarMenuComponent]
})
export class NavbarModule { }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With