Disable a radio button conditionally in a radio button group inside Reactive form in Angular 4

nir*_*aft 4 forms reactive-forms angular angular-reactive-forms angular4-forms

I have a reactive form created in component and some formControls defined on it.

Now i need to disable one of the radio button from a radio button group based on some condition.

How do i do it while creating formControl?

PFB the code snippet:

createForm() {
this.heroForm = this.fb.group({
  confirmActionRadioGrp: this.fb.group({
            updateAction: new FormControl({ value: '' })
        })

  });
}
Run Code Online (Sandbox Code Playgroud)

inside my template:

    <div class="form-group confirm-action-radio-container" formGroupName="confirmActionRadioGrp">
<input  type="radio"  class="custom-control-input" value="1" formControlName="updateAction">
    <input  type="radio"  class="custom-control-input" value="2" formControlName="updateAction">
    <input  type="radio"  class="custom-control-input" value="3" formControlName="updateAction"></div>
Run Code Online (Sandbox Code Playgroud)

Now how to disable one of them conditionally? when using [disable] attribute i get some warning in browser console. Angular doesn't encourage doing it

bry*_*n60 6

忽略浏览器警告并在输入中使用 disabled 属性。如果您愿意,可以使用:

[attr.disabled]="whatever"
Run Code Online (Sandbox Code Playgroud)

相反,这应该使警告静音。根据此未解决的问题,Angular 反应式表单目前不支持通过 formcontrol / formgroup API 禁用组中的单个单选按钮:

https://github.com/angular/angular/issues/11763

  • 使用 `[attr.disabled]` 也对我有用。不知道为什么这有一个downvote。请注意,为了启用(而不是禁用)输入,您需要返回 `null` 而不是 `false`。 (4认同)