[英]angular directive is not working
我有一个文本框和一条指令来限制在屏幕上显示负值,并且ng-model应该包含该负值。
在下面的指令,我总是得到""
作为inputValue的价值,如何获得的inputValue将在NG-模型值
app.directive('numberOnly', function () {
return {
require: 'ngModel',
link: function (scope, element, attrs, modelCtrl) {
modelCtrl.$parsers.push(function (inputValue) {
if (inputValue == undefined) return '';
var transformedInput = inputValue.replace(/[^0-9]/g, '');
if (transformedInput != inputValue) {
modelCtrl.$setViewValue(transformedInput);
modelCtrl.$render();
}
return transformedInput;
});
}
};
});
您的代码运行良好:
<div ng-app="myModule">
<div ng-controller="myController">
<input type="text" number-only ng-model="model"/>
</div>
</div>
var module = angular.module("myModule", []);
module.controller('myController', ['$scope', function($scope) {
$scope.model = '01234';
}]);
module.directive('numberOnly', function() {
return {
require: 'ngModel',
link: function(scope, element, attrs, modelCtrl) {
modelCtrl.$parsers.push(function(inputValue) {
// Value prints here
console.log(inputValue);
if (inputValue == undefined) return '';
var transformedInput = inputValue.replace(/[^0-9]/g, '');
if (transformedInput != inputValue) {
modelCtrl.$setViewValue(transformedInput);
modelCtrl.$render();
}
return transformedInput;
});
}
};
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.