Angular form updateValueAndValidity of all children controls

后端 未结 5 649
攒了一身酷
攒了一身酷 2020-12-29 01:50

In my Angular 4 app, I have a form with several controls.

At some points I need to force the update of their validity, so I\'m doing:

this.form.get(\         


        
5条回答
  •  [愿得一人]
    2020-12-29 02:42

    validateFormFields(fields) {
        try {
          Object.entries(fields.controls).forEach((control: any) => {
            if (typeof control[0] == 'Array' 
            ) {
              this.validateFormFields(control[1]);
            } else {
              if (control[1].controls) {
                Object.entries(control[1].controls).forEach((c: any) => {
                  c[1].touched = true;
                });
              } else {
                control[1].touched = true;
              }
    
            }
          });
        } catch (e) {
          console.log(e);
        }
      }
    

提交回复
热议问题