Angular form updateValueAndValidity of all children controls

后端 未结 5 665
攒了一身酷
攒了一身酷 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

    I had the same situation for me to update FormGroup | FormArray at nested level controls.

    check this out(worked for me):

    /**
     * Re-calculates the value and validation status of the entire controls tree.
     */
    function updateTreeValidity(group: FormGroup | FormArray): void {
      Object.keys(group.controls).forEach((key: string) => {
        const abstractControl = group.controls[key];
    
        if (abstractControl instanceof FormGroup || abstractControl instanceof FormArray) {
          updateTreeValidity(abstractControl);
        } else {
          abstractControl.updateValueAndValidity();
        }
      });
    }
    

提交回复
热议问题