AsyncValidators bug using directives
A sort of issue I found in AngularJS. I'm not sure if it's a bug.
HTML
<div ng-app="myApp" ng-controller="myController">
<form name="myForm">
This value is safe and will propagate changes every time: <br>
{{data.safeValue}}<br>
<input type="text" ng-model="data.safeValue" name="safeValue"><br>
This value is unsafe because of the update to asyncValidators and will break<br>
{{data.unsafeValue}} <br>
<input type="text" ng-model="data.unsafeValue" add-async name="unsafeValue"><br>
</form>
<button ng-click="resolve(myForm)"> Resolve </button>
<button ng-click="reject(myForm)"> Reject </button>
</div>
JavaScript
console.log(angular.version.full)
angular.module('myApp', [])
.directive('addAsync', function($q){
return {
require: 'ngModel',
link: function(scope, element, attr, ctrl) {
ctrl.$asyncValidators.breaking = function(modelValue, viewValue) {
if (ctrl.$isEmpty(modelValue)) {
return $q.when();
}
ctrl.deferred = $q.defer();
ctrl.promise = ctrl.deferred.promise;
return ctrl.deferred.promise;
};
}
}
})
.controller('myController', function($q, $scope){
var self = this;
$scope.resolve = function(form){
form.unsafeValue.deferred.resolve();
};
$scope.reject = function(form) {
form.unsafeValue.promise
.catch(function(){
form.unsafeValue.$modelValue = form.unsafeValue.$viewValue;
});
form.unsafeValue.deferred.reject();
};
$scope.data = {
safeValue: "foo",
unsafeValue: "bar"
};
});