typescript 使原始的 Angular 表单控件变脏
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44508982/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Make pristine Angular form control dirty
提问by Estus Flask
There is a reactive form in Angular 4, and some control is supposed to be set programmatically at some point.
在 Angular 4 中有一个反应形式,并且应该在某个时候以编程方式设置一些控制。
this.form = formBuilder.group({
foo: ''
});
...
this.form.controls.foo.setValue('foo');
How can control pristine/dirty state be affected? Currently I'm using both form
and foo
pristine states, something like:
如何控制原始/脏状态?目前我同时使用form
和foo
原始状态,例如:
<form [formGroup]="form">
<input [formControl]="form.controls.foo">
</form>
<p *ngIf="form.controls.foo.pristine">
{{ form.controls.foo.errors | json }}
</p>
<button [disabled]="form.pristine">Submit</button>
If pristine/dirty is supposed to designate only human interaction and can't be changed programmatically, what solution would be preferable here?
如果 pristine/dirty 应该只指定人类交互并且不能以编程方式更改,那么这里最好的解决方案是什么?
回答by Andriy
Each instance of formControl
has markAsDirty()
and markAsPristine()
methods, so, you should be able to run
formControl
hasmarkAsDirty()
和markAsPristine()
方法的每个实例,所以,你应该能够运行
this.form.controls.foo.markAsPristine()
or better, using reactive forms API:
或者更好,使用反应式表单 API:
this.form.get('foo').markAsPristine()
or even
甚至
this.form.markAsPristine()
the same may be done with markAsDirty()
method
可以用markAsDirty()
方法做同样的事情