[英]Validation not working (Directive scope.$error is not populated although ng-invalid class is applied to DOM element)
Updated: also, if anyone can tell me how to simplify my code, I would really appreciate that. 更新:另外,如果有人可以告诉我如何简化代码,我将非常感谢。
In short: I have simple validation rule applied to element 简而言之:我对元素应用了简单的验证规则
<div ng-form='myForm'>
<input ng-model='row.item[0].field' required />
</div>
I also have style to color invalid entry 我也有为无效条目上色的样式
.ng-invalid { background:red }
And when I remove value from input box it's background color is changed, however neither row
nor row.item
, row.item[0]
and row.item[0].field
has $error
property appeared. 当我从输入框中删除值时,它的背景色发生了变化,但是
row
和row.item
, row.item[0]
和row.item[0].field
row.item[0]
都没有出现$error
属性。 myForm.$error
doesn't have anything as well. myForm.$error
也没有任何内容。
So I cannot print validation message bellow input box 所以我无法在下面的输入框中打印验证消息
Longer explanation: 更长的解释:
I could have much broader problem than that. 我可能会遇到比这更广泛的问题。 Here is my setup (simplified) :
这是我的设置(简化):
Markup: 标记:
Code: 码:
function tableCtrl($scope) {
var fields = $scope.fields = [
{ name: 'events', required: true }
,{ name: 'subjects' }
,{ name: 'affected'}]
$scope.events = [{ name : 'e', type :'some', organ :'blood'
, items : [{ events : 1, subjects: 2, affected : 3 }
,{ events : 1, subjects: 2, affected : 3 }
,{ events : 1, subjects: 2, affected : 3 } ] }
, { name:'f', type : 'any', organ :'heart'
, items :[{ events : 1, subjects: 2, affected : 3 }
,{ events : 1, subjects: 2, affected : 3 }
,{ events : 1, subjects: 2, affected : 3 } ]}
, { name: 'g', type: 'all', organ :'skin'
, items : [{ events : 1, subjects: 2, affected : 3 }
,{ events : 1, subjects: 2, affected : 3 }
,{ events : 1, subjects: 2, affected : 3 } ]}]
}
angular.module('components').directive('editor', function ($compile) {
return {
scope : {
row : '=editor'
, fields : '=fields'
},
terminal:true,
link: function (scope, element, attrs) {
var tmpl = ''
for (var g in row.items ) {
var itemPath = 'row.items['+g+']'
for (var f in scope.fields) {
tmpl += '<td><input type="number" ng-model="'+itemPath+'.'+scope.fields[f].name + '"' +
' min="0" ' +
(scope.fields[f].required? ' required ' : '' )+
' /></td>'
}
}
var newElement = angular.element(tmpl);
$compile(newElement)(scope);
element.replaceWith(newElement);
}
}
So I'm creating input boxes dynamically. 因此,我正在动态创建输入框。
The validation is related to ng-form and ng-model directive which means you need either a <form>
element or <ng-form
element with a name for validation to work. 验证与ng-form和ng-model指令相关,这意味着您需要具有名称的
<form>
元素或<ng-form
元素,验证才能起作用。
Then you can access the valid state in the scope using formname.$error
. 然后,您可以使用
formname.$error
访问范围中的有效状态。
<form name="myform">
<input ng-model='row.item[0].field' required />
<input type="submit" ng-disabled="myform.$error" value="Save" />
</form>
Try to compile the new element on the parent scope, not on the isolated scope. 尝试在父作用域而不是隔离作用域上编译新元素。 It worked on my case.
它适用于我的情况。
$compile(newElement)(scope.$parent);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.