In short, the idea is to simplify templating without manually adding ng-class={'has-error': 'formName.inputName.$invalid'}to eachform-group
So, I want to create a directive that will generate a string to be added to the template element. This string is an attribute ng-classwith expression
I thought that creating a quick directive that adds the ng-class attribute during the compilation phase would be enough, but doesn't seem to shorten it.
Directive Definition Object
{
restrict: 'C',
compile: function(tElement, tAttrs) {
var $elem = angular.element(tElement),
formName = $elem.parents('[ng-form]').length ? $elem.parents('[ng-form]').attr('ng-form') : $elem.parents('form').attr('name'),
controlName = $elem.find('.form-control').attr('name');
$elem.attr('ng-class', '{"has-error": ' + formName + '.' + controlName + '.$invalid}');
console.log('added ng-class attr', $elem.attr('ng-class'));
}
}
See console
The first form-groupadds an attribute ng-classadded dynamically.
The corresponding expression is correct. But the directive is ng-classnever executed
form-group ng-class, , obvisouly
-
?