You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Need to be able to specify providers for standalone Pipes #49043
One thing that should be noted is that providing a service on a NgModule level is roughly equivalent of providing a service via @Injectable(providedIn: 'root'). In other words - providers are not scoped to a NgModule (this is a common misconception I see) but rather all providers are folded into the root injector (!). So you could just register the desired pipe on the root / when bootstrapping an application - this would be equivalent to registering it on a NgModule, really.
Then, pipes are meant to be used as a formatting facility in a template, not as generic providers. We never intended pipes to act as providers. In the particular case of the DatePipe you would be better off reusing the formatDate function directly.
I'm going to close this one as I don't think we want to introduce providers on a pipe level. The NgModule equivalent behaviour can be achieved today without NgModules and an alternative (arguably better) solution exists for the DatePipe
@pkozlowski-opensource Thanks for your quick response. However, I'd like to share my feedback:
Although what you've written is true, Angular still for whatever reason provides a way to define providers as an option to @Component() and @Directive() decorator calls. But this is not an option for @Pipe() Is there any conceptual reason for why @Pipe() should be different?
Then, pipes are meant to be used as a formatting facility in a template, not as generic providers. We never intended pipes to act as providers.
If the Angular team never intended pipes to act as providers, then Angular has an issue that it does not provide a clean way to reuse an existing pipe by another pipe. This issue should be addressed IMO.
In the particular case of the DatePipe you would be better off reusing the formatDate function directly.
I'd love to be able to use the pipe itself, because formatDate is different from DatePipe: it has 3 non-optional parameters instead of 1, and in order to be able to use it the same way as formatDate I would need to inject even more stuff into my custom pipe.
But this is not an option for @pipe() Is there any conceptual reason for why @pipe() should be different?
Yes, pipes don't have "children" in a template.
If the Angular team never intended pipes to act as providers, then Angular has an issue that it does not provide a clean way to reuse an existing pipe by another pipe. This issue should be addressed IMO.
Pipes are really not meant to be composed - same as you can't compose, say, binding syntax. This could be seen as a limitation or a design choice.
I'd love to be able to use the pipe itself, because formatDate is different from DatePipe: it has 3 non-optional parameters instead of 1, and in order to be able to use it the same way as formatDate I would need to inject even more stuff into my custom pipe.
Understood - the formatDate is more flexible here and while might not be 100% what you are looking for it is a reasonable path forward.
Thnx for sharing your input / thinking - appreciate it - even if the providers on the pipe level are not what we want to support.
Well, Directives don't have a template neither, but they do have the providers property.
Pipes are really not meant to be composed - same as you can't compose, say, binding syntax. This could be seen as a limitation or a design choice.
Got it, fair enough. Although it could be a point for Angular improvement.
Generally, thanks for your time and appreciation. I just really believe that the Angular mental model could benefit from a simplification by removing unreasonable (IMO) differences like this one.
angular-automatic-lock-bot commented on Mar 16, 2023
Activity
pkozlowski-opensource commentedon Feb 13, 2023
One thing that should be noted is that providing a service on a NgModule level is roughly equivalent of providing a service via
@Injectable(providedIn: 'root'). In other words - providers are not scoped to a NgModule (this is a common misconception I see) but rather all providers are folded into the root injector (!). So you could just register the desired pipe on the root / when bootstrapping an application - this would be equivalent to registering it on a NgModule, really.Then, pipes are meant to be used as a formatting facility in a template, not as generic providers. We never intended pipes to act as providers. In the particular case of the
DatePipeyou would be better off reusing the formatDate function directly.I'm going to close this one as I don't think we want to introduce providers on a pipe level. The
NgModuleequivalent behaviour can be achieved today without NgModules and an alternative (arguably better) solution exists for theDatePipeMaximaximum commentedon Feb 13, 2023
@pkozlowski-opensource Thanks for your quick response. However, I'd like to share my feedback:
Although what you've written is true, Angular still for whatever reason provides a way to define providers as an option to
@Component()and@Directive()decorator calls. But this is not an option for@Pipe()Is there any conceptual reason for why@Pipe()should be different?If the Angular team never intended pipes to act as providers, then Angular has an issue that it does not provide a clean way to reuse an existing pipe by another pipe. This issue should be addressed IMO.
I'd love to be able to use the pipe itself, because
formatDateis different fromDatePipe: it has 3 non-optional parameters instead of 1, and in order to be able to use it the same way asformatDateI would need to inject even more stuff into my custom pipe.pkozlowski-opensource commentedon Feb 13, 2023
Yes, pipes don't have "children" in a template.
Pipes are really not meant to be composed - same as you can't compose, say, binding syntax. This could be seen as a limitation or a design choice.
Understood - the
formatDateis more flexible here and while might not be 100% what you are looking for it is a reasonable path forward.Thnx for sharing your input / thinking - appreciate it - even if the providers on the pipe level are not what we want to support.
Maximaximum commentedon Feb 13, 2023
Well, Directives don't have a template neither, but they do have the
providersproperty.Got it, fair enough. Although it could be a point for Angular improvement.
Generally, thanks for your time and appreciation. I just really believe that the Angular mental model could benefit from a simplification by removing unreasonable (IMO) differences like this one.
angular-automatic-lock-bot commentedon Mar 16, 2023
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.