Disable Input fields in reactive form

后端 未结 16 1083
暖寄归人
暖寄归人 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 20:57

    A more general approach would be.

    // Variable/Flag declare
    public formDisabled = false;
    
    // Form init
    this.form = new FormGroup({
      name: new FormControl({value: '', disabled: this.formDisabled}, 
        Validators.required),
     });
    
    // Enable/disable form control
    public toggleFormState() {
        this.formDisabled = !this.formDisabled;
        const state = this.formDisabled ? 'disable' : 'enable';
    
        Object.keys(this.form.controls).forEach((controlName) => {
            this.form.controls[controlName][state](); // disables/enables each form control based on 'this.formDisabled'
        });
    }
    

提交回复
热议问题