设置为脏数据的字段来触发角JS验证 [英] Setting fields to dirty to trigger validations in Angular JS
问题描述
我想获得的所有字段的形式,并将它们设置脏。
I am trying to get all the fields in a form and set them dirty.
事情是这样的:
$timeout(function () {
$('input').each(function(){
$(this).$dirty=true;
});
}, 0);
这是行不通的。我不明白,像这样的作品就好了:
This is not working. I do understand that something like this works just fine:
$scope.form.uName.$dirty = true;
但是,当我使用jQuery在所有领域的循环,不顺心的事。
But when I iterate through all the fields using jquery, something goes wrong.
下面是小提琴: http://jsfiddle.net/Qdk5M/1302/
鸭preciate任何帮助。谢谢。
Appreciate any help. Thank you..
推荐答案
创建一个链接指令
函数,通过表单属性迭代。用一根火柴,以免其中含有 $
去仅表单字段任何形式的对象属性。将 $脏
的这些属性真正
:
Create a directive with a link
function that iterates through the form properties. Use a match to avoid any form object property which contains a $
to get to only the form fields. Set the $dirty
property of these to true
:
.directive('makeDirty', function(){
return function(scope, elem, attr) {
angular.forEach(scope.form, function(val, key){
if(!key.match(/\$/)) {
val.$dirty = true;
}
});
scope.form.$setDirty(); // you can make the form itself $dirty if you wish to
}
});
这里使用指令的优点在于,直到表格准备好它不会运行链路code;有一些小的重构,也有可能是跨多个表格/控制器重复使用。
The advantage of using a directive here is that it will not run the link code until the form is ready; with some minor refactoring, it could also be reused across multiple forms/controllers.
这篇关于设置为脏数据的字段来触发角JS验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!