1

I have the below code in my HTML. currencyCustomFormat is a custom Pipe. Now in this custom pipe file, i would like to access the E_CURRENCY value ( value could be AUD or NZD or any other currency fetched from service) . How can i access this.

@Pipe({
  name: 'currency'
})
export class CurrencyPipe implements PipeTransform {
  translateService
  @select(user) currentUser$
  constructor() {}
  transform(value, country): string {
    if (!value) {
      return ''
    } else {
      if (!country) {
        country = 'NZD'
      }
      const symbol = symbols[country] //value of symbol is $
      if (symbol) {
        this.currentUser$.first().subscribe(v => {
          this.translateService.use(v.E_LOCALE);

          const decimalSepValue = this.translateService.instant('CURRENCYFORMATS.' + currencyCode + '.DECIMALSEPARATOR');
          const thousandSepValue = this.translateService.instant('CURRENCYFORMATS.' + currencyCode + '.THOUSANDSEPARATOR');
          const currencyDecValue = this.translateService.instant('CURRENCYFORMATS.' + currencyCode + '.CURRENCYDECIMALS'); // **i need to pass the value of E_CURRENCY to currencyCode variable**
          return accounting.formatMoney(value, {
            symbol,
            format: '%s %v',
            precision: currencyDecValue,
            thousand: thousandSepValue,
            decimal: decimalSepValue
          })
        })
      };
      return accounting.formatMoney(value, {
        symbol: country,
        format: '%v %s'
      });
    }
  }
}
<div label="ORDER_DETAILS.TOTAL_PRICE" [value]="order.details.E_NET_PRICE | currencyCustomFormat: order.details.E_CURRENCY"></div>
31piy
  • 23,323
  • 6
  • 47
  • 67
angie
  • 13
  • 1
  • 7
  • Have you tried using standard dependency injection, adding the service to your constructor arguments? – Will Alexander Sep 10 '19 at 07:26
  • @WillAlexander Is there any other way to achieve this other than using dependency injection? – angie Sep 10 '19 at 07:41
  • I mean...maybe? But why? – Will Alexander Sep 10 '19 at 07:52
  • @WillAlexander Because i dont want to make a unnecessary service call. I already have the value in HTML, is there anyway i can pass the value from HTML to my pipe function directly( thorugh parameter passing something like that ) – angie Sep 10 '19 at 11:16
  • You can pass any arguments you like to your pipe, but making parts of the template depend on each other is a bad idea, so I wouldn't use values from the template. If you have them in another component, you can pass them around, but again, none of that is better than simply getting the value from the service. It won't change the performance in the slightest, because the service is already instantiated. – Will Alexander Sep 10 '19 at 11:33

0 Answers0