Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular custom pipe not be found

In my application I need a custom pipe globally, I try to implement it following angular pipe but i see always this error

Template parse errors: The pipe 'formatdate' could not be found

formatdate.pipe

import { Pipe, PipeTransform } from '@angular/core';

@Pipe({
  name: 'formatdate'
})

export class FormatdatePipe implements PipeTransform {

  transform(dateJson: any, args?: any): any {
.
 //code...
.
      return dateJson;
    }
  }
}

app.module

import { FormatdatePipe } from './shared/pipes/formatdate.pipe';
@NgModule({
  declarations: [
    AppComponent, FormatdatePipe 
  ],

This pipe works if I import it in all my module and not in the principal app.module, do I need a routin pipe module or something

like image 365
Alessandro Celeghin Avatar asked Jul 14 '17 08:07

Alessandro Celeghin


1 Answers

Pipes (like Components and Directives) don't work globally like services do.

You need to define a pipe in some module. Then you can use it in components defined in that module. Another way is to add the pipe to exports of a module and then import that module in the module where you want to use it.

Define it like this:

import { FormatdatePipe } from './shared/pipes/formatdate.pipe';

@NgModule({
  declarations: [
    FormatdatePipe 
  ],
  exports: [
    FormatdatePipe
  ]
})   
export class SomeUtilModule {}

Then import this module where you want to use it and it should work :)

like image 189
eddyP23 Avatar answered Oct 06 '22 03:10

eddyP23