Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Format currency in AngularJS in ngModel

Tags:

angularjs

I am trying to format currency when the page loads using AngularJS. But it doesn't work if I use the filter currency in ngModel.It seems to work only for {{var |currency}}.

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

like image 319
user1227313 Avatar asked Nov 11 '13 06:11

user1227313


1 Answers

Try this:

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';
}

Plunker example

If that does not help, check this: Getting a currency format pattern from AngularJS filter

like image 180
lukpaw Avatar answered Sep 19 '22 00:09

lukpaw