[英]Problems with angular validation in directive
I am fighting with the validation in an angular directive without success. 我正在与角度指令中的验证作斗争,但没有成功。
The form.name.$error object seems to be undefined, when I submit the name property to the directive template. 当我将name属性提交给指令模板时,form.name。$ error对象似乎未定义。 If i use a fixed name-attribute inside the template, the $error object is fine, but of course identical for all elements.
如果我在模板中使用固定的名称属性,则$ error对象可以,但是对于所有元素当然都相同。
The html is: 的HTML是:
<form name="form" novalidate>
<p>
<testvalidation2 name="field1" form="form" field="testfield4" required="true">
</testvalidation2>
</p>
</form>
The directive looks like this: 该指令如下所示:
app.directive('testvalidation2', function(){
return {
restrict: 'E',
scope: {
ngModel: '=',
newfield: '=field',
required: '=required',
form: '='
},
templateUrl: 'template2.html',
link: function(scope, element, attr){
scope.pattern = /\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b/;
scope.name = attr.name;
}
} // return });` } // return});`
and finally the template: 最后是模板:
<div>
<input name="{{name}}" type="text" ng-model="newfield" ng-required="required" ng-pattern="pattern"> {{FIELD}}</input>
<span ng-show="form.name.$error.required">Required</span>
<span ng-show="form.name.$error.pattern"> Invalid </span>
<p>Output {{form.name.$error | json}}</p>
</div>
I have created a plunker for my Angular Validation Problem and would be happy, if someone would help me to win the fight. 我为“ 角度验证问题”创建了一个小插曲,如果有人能帮助我赢得这场战斗,我会很高兴。
Michael 迈克尔
I don't have a fix for this but I can tell you what the problem is. 我没有解决办法,但是我可以告诉您问题出在哪里。
form="form"
should have name of the form form="form2"
. form="form"
应该具有form="form2"
。 input
control that you add would not get added to the parent scope form2
. input
控件不会添加到父作用域form2
。 The only way out currently i can think of is to not use isolated scope. 目前我能想到的唯一方法是不使用隔离范围。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.