作者热门文章
- xml - AJAX/Jquery XML 解析
- 具有多重继承的 XML 模式
- .net - 枚举序列化 Json 与 XML
- XML 简单类型、简单内容、复杂类型、复杂内容
我在 html 中
<input type="text" ng-model="price" >
<h2>{{ price | currency}}</h2>
在 Controller 中
$scope.price = 10;
Which displays **$10** in h1 if i change the value in price model input.
我希望文本框输入以货币形式输入(输入框中的 10 美元作为值)。如何实现?
最佳答案
您可以尝试使用 formatters和 parsers喜欢
app.directive('currency', function () {
return {
require: 'ngModel',
link: function(elem, $scope, attrs, ngModel){
ngModel.$formatters.push(function(val){
return '$' + val
});
ngModel.$parsers.push(function(val){
return val.replace(/^\$/, '')
});
}
}
})
然后
<input type="text" ng-model="price" currency>
演示:Fiddle
关于javascript - 如何将 ng-model 输入值显示为 Angular 货币?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20739694/
我是一名优秀的程序员,十分优秀!