我正在尝试使用AngularJS加载页面时格式化货币.但是如果我在ngModel中使用过滤器货币它就不起作用.它似乎只适用于{{var | currency}}.
PS:我希望它能在ngModel上工作,我希望在页面加载时格式化货币.
试试这个:
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>
Run Code Online (Sandbox Code Playgroud)
JavaScript的
angular.module('App', []);
function Ctrl($scope) {
$scope.amount1 = 1234.56;
$scope.symbol2 = 'USD$';
$scope.amount2 = '1234.56';
}
Run Code Online (Sandbox Code Playgroud)
如果这没有帮助,请检查: 从AngularJS过滤器获取货币格式模式
如果你想在 html 中而不是在 ng-model 中使用过滤器,这是一种替代方法:
<input type="text" ng-model="var" value="var|currency"/>
您可以将其添加到 value 属性。