简体   繁体   English

检查嵌套反应式 forms 中的(子)表单是否脏

[英]check if (child) form is dirty in nested reactive forms

I have a form with different sections (nested formgroups) How can you check if something changes in a specific section.我有一个包含不同部分的表单(嵌套表单组)如何检查特定部分中的某些内容是否发生变化。

HTML: HTML:

<div [formGroup]="formGroup">
  <div formGroupName="one">
    <input type="text" formControlName="email">
    <input type="text" formControlName="name">
   <div>
</div>

TS: TS:

 export class someClass implements OnInit {
  
  formGroup = this.formBuilder.group({
    one: this.formBuilder.group({
      email: [null, [Validators.required, Validators.pattern('^[a-z0-9._%+-]+@[a-z0-9.-]+\\.[a-z]{2,4}$')]],
      name[null],
      })
  });

 get emailControl(): AbstractControl { return this.formGroup.get('one.email'); }
 get nameControl(): AbstractControl { return this.formGroup.get('one.name'); }

...
}

for example if I want a class (style) if the form is dirty, I can do something like:例如,如果我想要一个 class (样式),如果表格很脏,我可以这样做:

[class.dirty]="formGroup.dirty" [class.dirty]="formGroup.dirty"

How can I check if the "one" form is dirty?如何检查“一个”表格是否脏?

Angular will automatically adds control class, If form is dirty, you can use that class to style as per your need. Angular 将自动添加控件 class,如果表格脏了,您可以使用该 class 来根据您的需要进行样式。

div.ng-dirty{
....
}

For More Information 了解更多信息

You can access the group dirtiness by calling您可以通过调用访问组脏

formGroup.get('one').dirty

That returns the FormGroup as AbstractControl , thus with standard control props accessible.这会将FormGroup作为AbstractControl返回,因此可以访问标准控件道具。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM