如果我理解正确,您可以对仅数字部分或您的问题使用指令,这是我编写和使用的指令,我不知道这是否会解决您的手表功能问题:
myApp.directive('numbersOnly', function() {
return {
require: 'ngModel',
link: function(scope, element, attrs, modelCtrl) {
modelCtrl.$parsers.push(function(inputValue) {
// this next if is necessary for when using ng-required on your input.
// In such cases, when a letter is typed first, this parser will be called
// again, and the 2nd time, the value will be undefined
if (inputValue == undefined)
return ''
var transformedInput = inputValue.replace(/[^0-9]/g, '');
if (transformedInput != inputValue) {
modelCtrl.$setViewValue(transformedInput);
modelCtrl.$render();
}
return transformedInput;
});
}
};
});
我还使用ngBlur
(AngularJS 1.2 之前的)指令来限制手表执行的回调,不知道这对您的实例是否有帮助。