Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to declare a pipe globally to use in different modules?

I have a custom pipe named CurrConvertPipe

import {Pipe, PipeTransform} from '@angular/core'; import {LocalStorageService} from './local-storage'; @Pipe({name: 'currConvert', pure: false}) export class CurrConvertPipe implements PipeTransform {   constructor(private currencyStorage: LocalStorageService) {}    transform(value: number): number {      let inputRate = this.currencyStorage.getCurrencyRate('EUR');     let outputputRate = this.currencyStorage.getCurrencyRate(localStorage.getItem('currency'));     return value / inputRate * outputputRate;   } } 

I need to use this in two different modules, Module1 and Module2.
When I import in Module1 and Module2, I get an error saying it should be declared in a higher level module.

So I declare the pipe inside the app.module.ts

import './rxjs-extensions'; import { NgModule } from '@angular/core'; import { BrowserModule } from '@angular/platform-browser'; import { FormsModule } from '@angular/forms'; import { AppRoutingModule } from './app-routing.module'; import { AppComponent } from './app.component'; import { CurrConvertPipe } from './services/currency/currency-pipe'; @NgModule({     imports: [         BrowserModule,         FormsModule,         HttpModule,         AppRoutingModule,         Module1,                  Module2      ],      declarations: [         AppComponent,         CurrConvertPipe     ],     providers: [      ],     bootstrap: [AppComponent] }) export class AppModule { } 

But when I use it in Module1, it throws an error

The pipe 'currConvert' could not be found

like image 293
Sajeetharan Avatar asked Mar 03 '17 10:03

Sajeetharan


People also ask

How do I add a pipe to a module?

Steps to create custom pipe in angularAdd a Class named CustomPipe. The custom pipe class should implement PipeTransform interface. now implement pipe transform method that accepts value agrument. Add this pipe to the module you want to use the pipe in component of e.g. app.

How do you declare a pipe?

1) Create a Pipe Class and decorate it with the decorator @Pipe. 2) Supply a name property to be used as template code name. 3) Register your Pipe in the module under declarations. 4) Finally, implement PipeTransform and write transformation logic.


1 Answers

In Angular a good technique for sharing common directives, components, pipes, etc. is to use a so called shared module.

Those modules declare and export common parts, to make them usable for any of your other modules.

The fundamentals section of the angular documentation is a very good read about shared modules.

Let's take as example your currConvert pipe.

  • Declare new NgModule called ApplicationPipesModule

  • Add the pipe to the declarations and exports arrays of the NgModule-decorator metadata

  • Add any modules that may be required for your pipe to work to the imports array

    // application-pipes.module.ts // other imports import { CurrConvertPipe } from './{your-path}';  @NgModule({   imports: [     // dep modules   ],   declarations: [      CurrConvertPipe   ],   exports: [     CurrConvertPipe   ] }) export class ApplicationPipesModule {} 
  • import the created ApplicationPipesModule module into the modules where your pipe is going to be used, by adding it to the imports array

    // my-module1.module.ts // other imports import { ApplicationPipesModule } from './{your-path}';     @NgModule({  imports: [    // other dep modules    ApplicationPipesModule  ],  declarations: [],  exports: [] }) export class MyModule1 {} 
like image 96
cyr_x Avatar answered Sep 27 '22 20:09

cyr_x