Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I add the currency symbol in a view, to a value pulled from a model?

Tags:

angularjs

I need to precede a value pulled from JSON with '$'. How do I do this?

I want

<span ng-model="item.value"></span>

to render $12.34, given that in the model the value of item.value = 12.34

like image 936
Roger Creasy Avatar asked Oct 20 '22 14:10

Roger Creasy


1 Answers

Thanks to @JBNizet I found the answer! I posted below, in case this may help someone else in the future.

<span ng-bind="item.value | currency:USD$:2"></span>

I had to change 'ng-model' to 'ng-bind' and add the currency filter. For other newbies struggling with this - ng-model is two-way data binding, used when you user will be making changes to the data (via a form input for example). ng-bind is one-way data binding for pulling data, data that won't be changed, from the model.

Filters cannot be applied to ng-model; you instead create a formatter.

I hope this helps.

like image 85
Roger Creasy Avatar answered Oct 22 '22 22:10

Roger Creasy