Last active
November 11, 2015 12:55
-
-
Save uhtred/7bc27d17f9e65ef79004 to your computer and use it in GitHub Desktop.
Has Error Directive
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (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