我想在模型中更改输入元素和赋值的值,在异步验证器指令中。
angular
.module('lion.Form_Validator')
.directive('phoneValidation', Lion_Form_Validator_Phone)
function Lion_Form_Validator_Phone($q, $http) {
return {
restrict: 'A',
require: 'ngModel',
link: function (scope, element, attrib, ngModel) {
ngModel.$asyncValidators.phone = function (modelValue, viewValue) {
if (ngModel.$isEmpty(modelValue))
return $q.when();
$http.get('/?check-phone-numbers=' + modelValue, {cache: true}).then(function (res) {
if (!res.data.valid) {
deferred.reject();
} else {
/*********************/
// here want change the value of input and model
//element.val(res.data.phone); -> this not update the model
deferred.resolve();
}
});
return deferred.promise;
};
}
};
}
示例HTML代码:
<div data-ng-controller="BalanceController as balance">
<input type="text" name="phone" id="phone" data-phone-validation="" data-ng-model="balance.form.phone">
</div>