Skip to content

Need to be able to specify providers for standalone Pipes #49043

Closed as not planned

Description

@Maximaximum

Which @angular/* package(s) are relevant/related to the feature request?

core

Description

Currently there is no way to reuse an existing pipe or service inside a standalone pipe.

Proposed solution

We should be able to specify providers for a pipe, just like we do for components and directives

@Pipe({
  name: 'formControlValue',
  providers: [DatePipe]
})
export class FormControlValuePipe implements PipeTransform {
  constructor(
    private datePipe: DatePipe
  ) {}

  transform(value: any, type: string) {
    switch (type) {
      case 'date':
        return this.datePipe.transform(value as string);
    }

    return value;
  }
}

Alternatives considered

The only workardound available is using a non-standalone pipe:

@NgModule({
  declarations: [FormControlValuePipe],
  exports: [FormControlValuePipe],
  providers: [DatePipe],
})
export class FormControlValueModule {}

@Pipe({
  name: 'formControlValue',
})
export class FormControlValuePipe implements PipeTransform {
  constructor(
    private datePipe: DatePipe
  ) {}

  transform(value: any, type: string) {
    switch (type) {
      case 'date':
        return this.datePipe.transform(value as string);
    }

    return value;
  }
}

However, this forces developers to abstain from using standalone pipes and sticking to otherwise redundant NgModules.

Activity

  1. pkozlowski-opensource commented on Feb 13, 2023

    @pkozlowski-opensource
    Member

    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

  2. Maximaximum commented on Feb 13, 2023

    @Maximaximum
    Author

    @pkozlowski-opensource Thanks for your quick response. However, I'd like to share my feedback:

    1. 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?

    2. 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.

    1. 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.

  3. pkozlowski-opensource commented on Feb 13, 2023

    @pkozlowski-opensource
    Member

    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.

  4. Maximaximum commented on Feb 13, 2023

    @Maximaximum
    Author

    Yes, pipes don't have "children" in a template.

    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.

  5. angular-automatic-lock-bot commented on Mar 16, 2023

    @angular-automatic-lock-bot

    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.

  6. locked and limited conversation to collaborators on Mar 16, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions