angularjs - How do I access the child ngModel from a directive? -
like in this question, want add .error
on form field's parent .control-group
when scope.$invalid
true.
however, hardcoding form name in ng-class="{ error: formname.fieldmodel.$invalid }"
means can't reuse in different forms, plus i'd rather not repeat declaration everywhere.
i figured directive looks work:
<div class="control-group" error-on="model1, model2"> <input ng-model="model1"> <input ng-model="model2"> </div>
so when either model1
or model2
not valid, .control-group
gets .error
added.
my attempt here. possible access models directive, given model names?
if there's better approach, i'd love hear too.
i don't think writing custom directive necessery use-case ng-form
directive created situations those. directive's documentation:
it useful nest forms, example if validity of sub-group of controls needs determined.
taking code example 1 write:
<div class="control-group" ng-class="{ error: mycontrolgroup1.$invalid }> <ng-form name="mycontrolgroup1"> <input ng-model="model1"> <input ng-model="model2"> </ng-form> </div>
by using technique don't need repeat expressions used in ng-model , can reuse fragment inside form.
Comments
Post a Comment