我的应用程序中有一个文本框,我正在使用“模糊时更改”自定义指令来验证制表符上的该字段,即,如果我最初在该文本框中键入ABCD,则删除D字符,然后再次键入D字符,因此自定义模糊更改将仅调用一次。
对于某些问题,我使用ng-model-options="{updateOn: 'blur'}" ng-change="validate(data)"
代替ng-blur作为该元素。因此,如果我键入ABCD,它将调用该函数,再次删除并键入D字符,它将再次调用该函数。是否有自定义的ng-change指令只能调用一次。
最近2天,我一直在为此苦苦挣扎。请帮助
HTML:
<input type="text" ng-model="data.taxNo" id="taxNo"
ngomitsplchar class="form-control taxNo"
maxlength="15" style="text-transform: uppercase;"
ng-paste="$event.preventDefault();"
ng-model-options="{updateOn: 'blur'}"
ng-change="validate(data)"
tabindex="5" spellcheck="false" autocomplete="nope">
JS:
$scope.validate = function (obj) {
alert("2"); // Prompting alert 2 times when using ng-change
// Prompting alert 1 times when using change-on-blur
}
app.directive('changeOnBlur', function($timeout) {
return {
restrict: 'A',
require: 'ngModel',
link: function(scope, elm, attrs, ngModelCtrl) {
if (attrs.type === 'radio' || attrs.type === 'checkbox')
return;
var expressionToCall = attrs.changeOnBlur;
var oldValue = null;
elm.bind('focus',function() {
//scope.$apply(function() {
oldValue = elm.val();
//});
});
elm.bind('blur', function() {
$timeout(function() {
var newValue = elm.val();
if (newValue !== oldValue){
scope.$eval(expressionToCall);
}
//alert('changed ' + oldValue);
});
});
}
};
});
答案 0 :(得分:0)
我找到了解决方案。需要从ng-model-options="{updateOn: 'blur'}"
更改为ng-model-options="{updateOn: '**change blur**'}"
。因此,当使用ng-change="validate(data)"
<input type="text" ng-model="data.taxNo" id="taxNo"
ngomitsplchar class="form-control taxNo"
maxlength="15" style="text-transform: uppercase;"
ng-paste="$event.preventDefault();"
ng-model-options="{updateOn: 'change blur'}"
ng-change="validate(data)"
tabindex="5" spellcheck="false" autocomplete="nope">