Disable Input fields in reactive form

后端 未结 16 1027
暖寄归人
暖寄归人 2020-12-22 20:28

I already tried to follow the example of other answers from here and I did not succeed!

I created a reactive form (ie, dynamic) and I want to disable some fields at

16条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-22 21:11

    You can declare a function to enable/disable all of the form control:

      toggleDisableFormControl(value: Boolean, exclude = []) {
        const state = value ? 'disable' : 'enable';
        Object.keys(this.profileForm.controls).forEach((controlName) => {
          if (!exclude.includes(controlName))
            this.profileForm.controls[controlName][state]();
        });
      }
    

    and use it like this

    this.toggleDisableFormControl(true, ['email']);
    // disbale all field but email
    

提交回复
热议问题