我想格式化<input type="number" ng-model="volume">
,以便使用angular&#39; s指令显示带有2位小数的浮点数。我试过这个
angular.module('app').directive('input', function () {
return {
restrict: 'EA',
link: function (scope, element, attrs, ngModel) {
scope.$watch('ngModel', function(val){
ngModel = ngModel.toFixed(2)
});
}
};
});
&#13;
但由于我的ngModel不断返回controller {}
,因此无法正常工作。我不知道该怎么做。我尝试使用控制器的$ scope也没有结果。有什么建议吗?
我刚刚找到了最可能的答案:http://gsferreira.com/archive/2014/05/angularjs-smart-float-directive/并附带演示:http://jsfiddle.net/gsferreira/SCr6X/
它会自动将任意数字(包括整数)格式化为浮点数,用户仍然可以输入2个以上的精度数。它对我来说不是一个理想的解决方案