使原始的Angular窗体控件变脏 [英] Make pristine Angular form control dirty

查看:73
本文介绍了使原始的Angular窗体控件变脏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Angular 4中有一个反应形式,应该在某些时候以编程方式设置一些控件.

There is a reactive form in Angular 4, and some control is supposed to be set programmatically at some point.

this.form = formBuilder.group({
  foo: ''
});
...
this.form.controls.foo.setValue('foo');

如何控制原始/脏污状态?目前,我同时使用formfoo原始状态,例如:

How can control pristine/dirty state be affected? Currently I'm using both form and foo pristine states, something like:

<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?

推荐答案

每个formControl实例都具有markAsDirty()markAsPristine()方法,因此,您应该能够运行

Each instance of formControl has markAsDirty() and markAsPristine() methods, so, you should be able to run

this.form.controls.foo.markAsPristine()

或更好,使用反应形式API:

or better, using reactive forms API:

this.form.get('foo').markAsPristine()

甚至

this.form.markAsPristine()

可以使用markAsDirty()方法进行相同操作

the same may be done with markAsDirty() method

这篇关于使原始的Angular窗体控件变脏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆