15

I'm trying to use a custom-built pipe in my Angular 4 project. Can't figure out why the compiler doesn't recognize my pipe in the component template.

QFormat pipe

@Pipe({  name: 'qFormat'})
export class QFormatPipe implements PipeTransform {...}

SharedModule

@NgModule({
    declarations: [QFormatPipe],
    exports: [QFormatPipe]
})
export class SharedModule { }

FeatureModule

@NgModule({
  imports: [SharedModule],
  declarations: [MyComponent]
})
export class FeatureModule{ }

MyComponent.html

<div>{{someProp | qformat}}</div>
                  ^^^^^^^

The template throws error:

Angular: the pipe 'qformat' could not be found

Am I missing something?

BeetleJuice
  • 39,516
  • 19
  • 105
  • 165

3 Answers3

8

In my case the problem was that generating through CLI was adding pipe inside app.module.ts but the module of the component I was working in was different. Or in your case it could be not registered pipe/wrongly registered pipe in module you're using it.

So a quick solution is:

Delete the pipe you've created (Don't forget to remove it from app.module.ts). Then create a folder like pipes: Then generate a module for it like 'ng g module pipes/pipes-common'

Then generate you pipe like 'ng g pipe myPipeExample` and now don't forget to add it

declarations:[myPipeExamplePipe] array and

exports:[myPipeExamplePipe] array

Now import this module inside the module where you want to have it working.

This question has multiple very helpful answers

Black Mamba
  • 13,632
  • 6
  • 82
  • 105
6

The reason it can't find the pipe is because you registered the pipe as 'qFormat' with a capital F but in the HTML you have 'qformat' with a lowercase f. This is why the error is occurring. Your shared module registration and import/export is completely on point. The HTML should be:

<div>{{someProp | qFormat}}</div>

Hopefully that helps!

Alexander Staroselsky
  • 37,209
  • 15
  • 79
  • 91
2

mine was as simple as just forgetting to register it to my @NgModule

bresleveloper
  • 5,940
  • 3
  • 33
  • 47