I have the following Angular / HTML that uses Bootstrap CSS classes to indicate whether a form is valid or not using Angular validation.
<form name="editor" novalidate> <div class="form-group" ng-class="{'has-error': editor.name.$dirty && (editor.name.$error.invalid || editor.name.$error.required)}"> <input type="text" class="form-control" name="name" maxlength="150" data-ng-model="name" required /> </div> </form>
With more than one div.form-group , obviously a lot of code repetition. What I would like to do is create an Angular attribute directive that will update the div.form-group element class if the input contained in it is invalid.
This is the markup I would like to switch to:
<div class="form-group" data-form-group data-input="editor.name"> ... </div>
I have the following directive wrapper, but I donβt know how to control the editor.name (or input ) attribute to update the class.
myApp.directive("formGroup", function () { return { restrict: "A", scope: { input: "@" }, replace: false, link: function (scope, elem, attrs) { } }; });
I suppose I need to put the appropriate code in the link function and possibly with $watch , but other than that I'm a little in the dark
source share