gpt4 book ai didi

javascript - 如何将 ng-model 输入值显示为 Angular 货币?

转载 作者:数据小太阳 更新时间:2023-10-29 04:32:27 24 4
gpt4 key购买 nike

我在 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 美元作为值)。如何实现?

最佳答案

您可以尝试使用 formattersparsers喜欢

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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com