Ecco un jsfiddle: http://jsfiddle.net/javajunkie314/r6oyLe26/3/
L'idea è che le direttive myValidator
e myErrorMessage
sincronizzare il loro messaggio di errore tramite la direttiva myErrorHandler
. Fastidiosamente, non riesco a trovare alcun modo per accedere al motivo passato a reject
dal validatore.
Modifica: Ho aggiornato il violino. Ora myValidator
utilizza due validatori asincroni. Ho anche creato un ValidatorPromise
per gestire l'aggiornamento del controller myErrorHandler
. Questo è come ho descritto nel mio commento qui sotto.
Il codice HTML:
<form ng-controller="testCtrl">
<div my-error-handler="">
<input type="text" name="foo" ng-model="foo.text" my-validator="">
<div my-error-message=""></div>
</div>
</form>
La JavaScript:
(function() {
var app = angular.module('myApp', []);
/* Wraps a promise. Used to update the errorHandler controller when the
* validator resolves. */
app.factory('ValidatorPromise', function ($q) {
return function (errorHandler, name, promise) {
return promise.then(
// Success
function (value) {
// No error message for success.
delete errorHandler.error[name];
return value;
},
// Failure
function (value) {
// Set the error message for failure.
errorHandler.error[name] = value;
return $q.reject(value);
}
);
};
});
app.controller('testCtrl', function ($scope) {
$scope.foo = {
text: ''
};
});
app.directive('myErrorHandler', function() {
return {
controller: function() {
this.error = {};
}
};
});
app.directive('myValidator', function ($timeout, $q, ValidatorPromise) {
return {
require: ['ngModel', '^myErrorHandler'],
link: function (scope, element, attrs, controllers) {
var ngModel = controllers[0];
var myErrorHandler = controllers[1];
ngModel.$asyncValidators.test1 = function() {
return ValidatorPromise(
myErrorHandler, 'test1',
$timeout(function() {}, 1000).then(function() {
return $q.reject('Fail 1!');
})
);
};
ngModel.$asyncValidators.test2 = function() {
return ValidatorPromise(
myErrorHandler, 'test2',
$timeout(function() {}, 2000).then(function() {
return $q.reject('Fail 2!');
})
);
};
}
};
});
app.directive('myErrorMessage', function() {
return {
require: '^myErrorHandler',
link: function (scope, element, attrs, myErrorHandler) {
scope.error = myErrorHandler.error;
},
/* This template could use ngMessages to display the errors
* nicely. */
template: 'Error: {{error}}'
};
});
})();
fonte
2015-01-18 03:32:46
Come si imposta asincrono validazione, quale validatore? – dfsq
esattamente la stessa domanda che avevo, che era stata lasciata senza risposta, nulla di costruito per farlo, quindi ho fatto un trucco. vediamo se riceve questa risposta dagli esperti questa volta – harishr
@dsfq, non dovrebbe avere importanza, ma qui c'è un link al mio validatore: https://github.com/exceptionless/Exceptionless.UI/blob/master/src/ componenti/validatori/search-filter-validator.js e un link al mio markup https://github.com/exceptionless/Exceptionless.UI/blob/master/src/components/search-filter/search-filter-directive.tpl .html # L10-L26 –