2
我想創建一個指令,驗證用戶通過異步Web請求給出的郵件。它工作正常,但問題是,每次用戶輸入一個字符時都會進行異步調用,我希望異步調用僅在模糊時完成,我該怎麼做?關於模糊角度的異步驗證指令JS
下面是指令代碼:
angular.module('myModule').directive('usernameValidator', function($http, $q) {
return {
require: 'ngModel',
link: function(scope, element, attrs, ngModel) {
ngModel.$asyncValidators.username = function(modelValue, viewValue) {
return $http.post('/username-check', {username: viewValue}).then(
function(response) {
if (!response.data.validUsername) {
return $q.reject(response.data.errorMessage);
}
return true;
}
);
};
}
};
});
我試圖把一切都在一個陣營
element.bind('blur', function() { ... })
但是,當代碼element.bind集團裏面我有一個完全怪異行爲:第一次我集中輸入,沒有異步調用,當我模糊沒有異步調用,當我回來對輸入的焦點我有異步調用每個字符..