admin管理员组文章数量:1291721
Usually, validating forms in angular is done like this:
<form name="form">
<input name="fruit" ng-model="ctrl.fruit" required>
<span class="error" ng-show="form.fruit.$error.required">
Fruit is required.
</span>
</form>
The thing is that I have multiple forms some of which have similar fields, so I want to refactor some of this logic into a directive:
<form name="form1">
<fruit-input></fruit-input>
</form>
<form name="form2">
<fruit-input></fruit-input>
</form>
The problem is that the logic to display or hide the field error message requires the template to have access to the form to evaluate the condition form.fruit.$error.required
. But my fruitInput
directive needs to work regardless of the form where it's inserted.
Is there a way for the directive to identify its parent form so I can use it in its (isolated) scope to show/hide the error message?
Edit (due to a response questioning the usefulness of the question): Evidently the example is simplified.
In reality, a field on the form may have many of the following: a label, a help text, tooltip on hover, links to documentation, the control itself, all the different validation messages for the different validation conditions. One of such controls can easily be 20 lines of code and be used in 4~5 different places. So it's worth refactoring it.
Usually, validating forms in angular is done like this:
<form name="form">
<input name="fruit" ng-model="ctrl.fruit" required>
<span class="error" ng-show="form.fruit.$error.required">
Fruit is required.
</span>
</form>
The thing is that I have multiple forms some of which have similar fields, so I want to refactor some of this logic into a directive:
<form name="form1">
<fruit-input></fruit-input>
</form>
<form name="form2">
<fruit-input></fruit-input>
</form>
The problem is that the logic to display or hide the field error message requires the template to have access to the form to evaluate the condition form.fruit.$error.required
. But my fruitInput
directive needs to work regardless of the form where it's inserted.
Is there a way for the directive to identify its parent form so I can use it in its (isolated) scope to show/hide the error message?
Edit (due to a response questioning the usefulness of the question): Evidently the example is simplified.
In reality, a field on the form may have many of the following: a label, a help text, tooltip on hover, links to documentation, the control itself, all the different validation messages for the different validation conditions. One of such controls can easily be 20 lines of code and be used in 4~5 different places. So it's worth refactoring it.
Share edited Apr 26, 2014 at 2:21 Juan Enrique Muñoz Zolotoochin asked Apr 26, 2014 at 1:58 Juan Enrique Muñoz ZolotoochinJuan Enrique Muñoz Zolotoochin 3,5893 gold badges35 silver badges40 bronze badges1 Answer
Reset to default 10Try this directive:
app.directive("fruitInput",function(){
return {
restrict:"E",
template:'<input name="fruit" ng-model="ctrl.fruit" required> ' +
' <span class="error" ng-show="form.fruit.$error.required"> ' + //form here is the parent form resolved dynamically and saved in the scope as a property
' Fruit is required. ' +
' </span>',
scope:{},
require:"^form", //inject parent form as the forth parameter to the link function
link:function (scope,element,attrs,form){
scope.form = form; //save parent form
}
}
})
DEMO
版权声明:本文标题:javascript - How to get the parent form of a from a custom form control directive in angular? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741535132a2383983.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论