8

I am using CurrencyPipe with my application,

The following works,

 <div class="price">{{123 | currConvert | currency:'USD':true:'3.2-2'}}</div>

Now i have to pass the currency from the model variable, this is what i am doing,

  ngOnInit() {
         this.selectedCurrency = 'USD';
     }

and in template,

   <div class="price">{{123 | currConvert | currency:{{selectedCurrency}}:true:'3.2-2'}}</div>

it gives me a template parse error. what is the issue.

Sajeetharan
  • 216,225
  • 63
  • 350
  • 396

2 Answers2

15

Don't nest {{}}

   <div class="price">{{123 | currConvert | currency:selectedCurrency:true:'3.2-2'}}</div>
Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
  • that works, is there a event for the component that i can use whenever there is a change in component say it could be a label, which event to use? – Sajeetharan Feb 17 '17 at 07:15
  • actually i have a parent component and i set the selectedcurrency in the parentcomponent , this pipe is applied in another component. i get the selectedcurrency through localstorage on the child component. but it does not seem to get the updated selectedCurrency – Sajeetharan Feb 17 '17 at 07:17
  • I would need to see more code. You can try to inject `constructor(private cdRef:ChangeDetectorRef){}` and call `this.cdRef.detectChanges();` after you update `selectedCurrency`. – Günter Zöchbauer Feb 17 '17 at 07:19
  • do i have to use local storage for that? ' – Sajeetharan Feb 17 '17 at 07:21
  • Sorry, don't know what you mean. You don't *have to* use local storage, for anything, if you don't want to. – Günter Zöchbauer Feb 17 '17 at 07:25
  • http://stackoverflow.com/questions/42291902/how-to-notify-the-parent-component-variable-changes-to-another-component – Sajeetharan Feb 17 '17 at 07:29
0

when item is possibly null:

   <div class="price">{{123 | currConvert |  currency: item?.currency ?? '' }}</div>
Emmanuel Osimosu
  • 5,625
  • 2
  • 38
  • 39