问题描述
我用编程的值更新一些字段我的形式,我想设置字段状态 $脏
。这样做是这样的:
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 $脏= 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
方法。我采用了棱角分明的版本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.
推荐答案
在你的情况, $ scope.myForm.username $ setViewValue($ scope.myForm.username $ viewValue);
的伎俩 - 它使双方的形式和领域脏,并添加相应的CSS类
In your case, $scope.myForm.username.$setViewValue($scope.myForm.username.$viewValue);
does the trick - it makes both the form and the field dirty, and appends appropriate CSS classes.
只是说实话,我发现从你的问题的链接在新的职位该解决方案的话题。它的工作非常适合我,所以我把这个在这里作为一个独立的答案,使之更容易被发现。
Just to be honest, I found this solution in new post in the topic from the link from your question. It worked perfectly for me, so I am putting this here as a standalone answer to make it easier to be found.
编辑:
上面的解决方案最适合角度版本升级到1.3.3。与1.3.4开始,你应该使用新暴露的API方法 $使用setDirty()
从 ngModel.NgModelController
。
Above solution works best for Angular version up to 1.3.3. Starting with 1.3.4 you should use newly exposed API method $setDirty()
from ngModel.NgModelController
.
这篇关于Angular.js表单域编程方式设置脏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!