question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to declare pipe at component level?

See original GitHub issue

I got a pipe that is used only in one component. So I wanted to declare pipe at this component level. Something like this:

@Component({
  selector: 'app-my-comp',
  templateUrl: './my-comp.component.html',
  changeDetection: ChangeDetectionStrategy.OnPush,
  declarations: [MyCustomPipe]
})
export class MyComponent {

}

However the Component annotation doesn’t support declarations.

Is there anyway to achieve this? I know I can declare pipe in the module but then other components will be “notified” about it and it will break encapsulation.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
Airbladercommented, Apr 8, 2019

It is pretty inconsistent as you can provide services at component level but why can’t you pipes?

Declaring and providing something are different concepts, so I’m not sure why this would be “inconsistent”. You can, in fact, provide a pipe on a component much like you can provide a service.

Is there any reason why you absolutely need to scope the pipe’s declaration to this component? Just don’t use it elsewhere. If you really want to bundle it closely together, wrap them both into a separate module and don’t export the pipe.

0reactions
angular-automatic-lock-bot[bot]commented, Sep 14, 2019

This issue has been automatically locked due to inactivity. Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Using Custom Pipes in a Component Class in Angular
It's easy to create custom pipes to use in your templates to modify interpolated values. You don't have to duplicate your code if...
Read more >
How to Use an Angular Pipe in a Component File - Medium
Create a simple Angular application via the Angular CLI by running the following command. ng new pipe-in-component --defaults. Using the pipe in ...
Read more >
How to create and call a pipe from the component in Angular 2?
If you want to call the pipe directly within the component class, you need to instantiate it and call its tranform method: @Component({...
Read more >
Adding Components, Pipes and Services to a Module
Our modules are usually made up of multiple components, services, directives and pipes. In this chapter we are going to extend the example...
Read more >
Custom Pipes in Angular With Examples
Create a file named custom.pipe.ts ; Use The Pipe decorator (@Pipe) to define the pipe name that will be used within the components....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found