6

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.

2
  • 2
    Can you show the actual code that doesn't work? Commented Nov 11, 2013 at 6:50
  • 1
    Can you show that code have you tried. In js filter can be done using syntax $filter('currency')(amount[, symbol]) Commented Nov 11, 2013 at 7:19

2 Answers 2

7

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

Sign up to request clarification or add additional context in comments.

1 Comment

Its not right way to format ngModel I am not seeing any ngModel in html which is using currency formator
3

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

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

You can add it to value attribute.

4 Comments

This is not working if the input type is number. any way to do that ?
If the input is a number, you will not be able to have the currency symbol on it.
Getting this error : The value must match to the following regular expression: -?(\d+|\d+\.\d+|\.\d+)([eE][-+]?\d+)? Details : input type="number" changes to type="text" Now getting no error but still not working.
This is not working for me. Is there any condition for value="var|currency" ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.