Enable When Checkbox is Check in Reactive Forms

旧城冷巷雨未停 提交于 2019-11-28 14:18:16

Have a look at the Demo & Src

Stack Blitz, Fork

Explanation:

  1. For enabling or disabling a input field, you need to use the following syntax [attr.disabled]="myForm.get('rows').value[i].checkbox_value ? null: ''"
  2. attr.disabled, here attr is for binding with properties which don't exist directly on an element
  3. myForm.get('rows').value[i].checkbox_value, is fairly straight forward access to a form control's value.

Another aproach is create a directive

@Directive({
  selector: '[enabledControl]'
})
export class EnabledControlDirective {

    @Input() set enabledControl(condition: boolean) {
        if (this.ngControl) {
            if (this.ngControl.control) {
                if (condition)
                    this.ngControl.control.enable();
                else
                    this.ngControl.control.disable();
            }
        }
  }
  constructor(private ngControl : NgControl ) {
  }
}

Then you can use like

<input class="col-md-6" formControlName="quantity"
     [enabledControl]="row.get('checkbox_value').value" >
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!