Format currency in AngularJS in ngModel

I am trying to format the currency when a page loads using AngularJS. But this does not work if I use the filter currency in ngModel.It seems to only work for {{var | currency}}.

PS: I want it to work on ngModel, and I want the currency to be formatted when the page loads.

+6
source share
2 answers

If you want to have the filter in html and not in the ng model, this is an alternative:

<input type="text" ng-model="var" value="var|currency"/>

You can add it to the value attribute.

+4
source

Try the following:

HTML

 <!doctype html> <html ng-app="App"> <head> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.0/angular.min.js"></script> <script type="text/javascript" src="script.js"></script> </head> <body> <div ng-controller="Ctrl"> <span>Amount1: {{amount1 | currency:"USD$"}}</span><br/> <span>Symbol2: <input ng-model="symbol2"/></span><br/> <span>Amount2: <input ng-model="amount2"/></span><br/> </div> </body> </html> 

Javascript

 angular.module('App', []); function Ctrl($scope) { $scope.amount1 = 1234.56; $scope.symbol2 = 'USD$'; $scope.amount2 = '1234.56'; } 

Plunger example

If this does not help, check the following: Get the currency format template from the AngularJS filter

+8
source

Source: https://habr.com/ru/post/957807/


All Articles