Currency pipe with a dynamic currency variable in angular2
Asked Answered
A

2

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.

Allerus answered 17/2, 2017 at 7:5 Comment(0)
D
15

Don't nest {{}}

   <div class="price">{{123 | currConvert | currency:selectedCurrency:true:'3.2-2'}}</div>
Ducal answered 17/2, 2017 at 7:6 Comment(6)
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?Allerus
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 selectedCurrencyAllerus
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.Venusian
do i have to use local storage for that? 'Allerus
Sorry, don't know what you mean. You don't have to use local storage, for anything, if you don't want to.Venusian
#42292402Allerus
T
0

when item is possibly null:

   <div class="price">{{123 | currConvert |  currency: item?.currency ?? '' }}</div>
Tumor answered 26/7, 2023 at 0:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.