Skip to content

Instantly share code, notes, and snippets.

@uhtred
Last active November 11, 2015 12:55
Show Gist options
  • Save uhtred/7bc27d17f9e65ef79004 to your computer and use it in GitHub Desktop.
Save uhtred/7bc27d17f9e65ef79004 to your computer and use it in GitHub Desktop.
Has Error Directive
(function() {
'use strict';
angular
.module('app')
.directive('hasError', function($timeout) {
return {
restrict: 'A',
require: '^form',
link: function(scope, iElement, iAttrs, formCtrl) {
$timeout(function() {
var inputName = iElement.find('[name]')
.attr('name'),
// support to sub forms
mainForm = _.isUndefined(formCtrl.$$parentForm.$submitted) ? formCtrl : formCtrl.$$parentForm;
if (!_.isUndefined(inputName) && formCtrl[inputName]) {
scope.$watch(function() {
if (formCtrl[inputName]) {
return (mainForm.$submitted || formCtrl[inputName].$dirty) && formCtrl[inputName].$invalid;
}
return false;
}, function(invalid) {
iElement.toggleClass('has-error', invalid);
});
}
});
}
};
});
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment