angular.module('fr.hasError', []) .directive('frHasError', ['$compile', function ($compile) { return { restrict: 'A', priority: 1000, terminal: true, compile: function (element, attrs) { element.removeAttr('fr-has-error'); const onEvent = (attrs.onDirty !== undefined) ? '$dirty' : '$touched'; const formCtrl = attrs.frHasError.substr(0, attrs.frHasError.lastIndexOf('.')); element.attr('ng-class', "{ 'has-error': " + attrs.frHasError + '.$invalid && (' + attrs.frHasError + '.' + onEvent + ' || ' + formCtrl + '.$submitted ) }' ); return { post: function (scope, elem) { $compile(elem)(scope); }, }; }, }; }]);