Angular.js 以编程方式将表单字段设置为脏 [英] Angular.js programmatically setting a form field to dirty
问题描述
我正在以编程方式使用值更新表单上的某些字段,并且我想将字段状态设置为 $dirty
.做类似的事情:
I am programmatically updating some of the fields on my form with a value and I would like to set the field state to $dirty
. Doing something like:
$scope.myForm.username.$dirty = true;
似乎不起作用.
有一种方法 $setPristine
可以用来重置字段的状态,但没有 $setDirty
方法?
There is a method $setPristine
that I can use to reset the state of the field but there isn't a $setDirty
method?
那么如何去做呢?
我看到了这篇文章https://groups.google.com/forum/#!topic/angular/NQKGAFlsln4 但我似乎找不到 $setDirty
方法.我使用的是 Angular 1.1.5 版.
I saw this post https://groups.google.com/forum/#!topic/angular/NQKGAFlsln4 but I can't seem to find the $setDirty
method. I am using Angular version 1.1.5.
推荐答案
从 AngularJS 1.3.4 开始,您可以在字段上使用 $setDirty()
(来源).例如,对于每个有错误并标记为必填的字段,您可以执行以下操作:
Since AngularJS 1.3.4 you can use $setDirty()
on fields (source). For example, for each field with error and marked required you can do the following:
angular.forEach($scope.form.$error.required, function(field) {
field.$setDirty();
});
这篇关于Angular.js 以编程方式将表单字段设置为脏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!