Angular 2 - 如何在自定义管道内使用内置管道

Vij*_*lik 5 angular-pipe angular2-custom-pipes angular

我想使用内置货币管道制作自定义货币管道。我想要使​​用的方式是{{ anyNumber | customCurrency }}.然后在我的自定义货币管道中,我想使用内置货币管道。我可以根据某些逻辑决定货币管道的参数,并且不想像{{anyNumber | currency:'USD':false:'1:0-0'}}.

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

@Pipe({
  name: 'customCurrency'
})
export class CustomCurrencyPipe implements PipeTransform {

  transform(value: any, args?: any): any {
      const defaultLocale = 'USD';
      const showSymbol = false;

      ......some logic here......      

      //HOW TO USE CURRENCY PIPE HERE?

 }

}
Run Code Online (Sandbox Code Playgroud)

小智 1

您应该将内置管道注入到自定义管道中,而不是使用默认值调用它的转换方法

@Pipe({
  name: 'customCurrency'
})
export class CustomCurrencyPipe implements PipeTransform {

  constructor(public currencyPipe: CurrencyPipe) {
  }

  transform(value: any, args?: any): any {
      const currencyCode = 'USD';
      const showSymbol = false;
      return currencyPipe.transform(value, currencyCode, showSymbol);
 }

}
Run Code Online (Sandbox Code Playgroud)