Angular 2: Iterate over reactive form controls

前端 未结 9 752
遥遥无期
遥遥无期 2020-12-02 07:23

I would like to markAsDirty all the controls inside of a FormGroup.

9条回答
  •  北海茫月
    2020-12-02 07:42

    Using @Marcos answer I created a function that can be called passing a formGroup as parameter and it marks every formGroup children controls to dirty, just to make it usable from more places around the code putting it inside a service, for example.

    public touchAllFormFields(formGroup: FormGroup): void {
        Object.keys(formGroup.controls).forEach((key) => {
            formGroup.get(key).markAsDirty();
        });
    }
    

    hope it helps ;)

提交回复
热议问题