[英]AngularJS: call directive on ng-Model disable
app.directive('updateDecimals', function($parse, $filter) { return { restrict: 'A', require: '?ngModel', link: function(scope, ele, attrs, ngModel) { scope.$watch(function () { ngModel.$render = function(){ var decimalDigits = $parse(attrs.updateDecimals)(scope); console.log(decimalDigits); var updatedValue = parseFloat(ngModel.$viewValue, 10); updatedValue = $filter('number')(updatedValue, decimalDigits); console.log(updatedValue); ngModel.$setViewValue(updatedValue); } }); } }; });
在上面的代碼中,禁用ngModel時未調用指令
如果要格式化該值,則應將格式化程序添加到ngModel。$ formatters。
var app = angular.module('angularTest', []);
app.controller('MainCtrl', ['$scope', 'filterFilter', 'orderByFilter', function($scope, filterFilter, orderByFilter) {
$scope.value = 1.23456789;
}]);
app.directive('updateDecimals', function($parse, $filter) {
return {
restrict: 'A',
require: '?ngModel',
link: function(scope, ele, attrs, ngModel) {
var decimalDigits = $parse(attrs.updateDecimals)(scope);
console.log(decimalDigits);
ngModel.$formatters.unshift(function(v) {
var updatedValue = parseFloat(v, 10);
console.log(updatedValue);
updatedValue = $filter('number')(updatedValue, decimalDigits);
return updatedValue;
});
}
};
});
如果要修改模型值,可以類似地使用ngModel。$ parsers。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.