Search code examples
htmlangularradio-buttonangular-template-variable

Angular: radio button [checked] not set with template reference variable


As the title suggests, I have a scenario where, if the "Select All" radio button is checked, it must check all of the radio buttons in the columns below. Unfortunately, this is not working.

Here is a sample:

<table>
  <thead>
    <th>Role</th>
    <th colspan="3">Access</th>
  </thead>
  <tbody>
    <tr>
      <td>Select All</td>
      <td>
        <input #none type="radio" name="access0" value="allNone"/>
        <label>None</label>
      </td>
      <td>
        <input #readOnly type="radio" name="access0" value="allReadOnly"/>
        <label>Read Only</label>
      </td>
      <td>
        <input #full type="radio" name="access0" value="AllFull"/>
        <label>Full</label>
      </td>
    </tr>
    <tr>
      <td>Admin</td>
      <td>
        <input type="radio" name="access1" value="None" [checked]="none.checked"/>
        <label>None</label>
      </td>
      <td>
        <input type="radio" name="access1" value="ReadOnly" [checked]="readOnly.checked"/>
        <label>Read Only</label>
      </td>
      <td>
        <input type="radio" name="access1" value="Access" [checked]="full.checked"/>
        <label>Full</label>
      </td>
    </tr>
    <tr>
      <td>Sales Person</td>
      <td>
        <input type="radio" name="access2" value="None" [checked]="none.checked"/>
        <label>None</label>
      </td>
      <td>
        <input type="radio" name="access2" value="ReadOnly" [checked]="readOnly.checked"/>
        <label>Read Only</label>
      </td>
      <td>
        <input type="radio" name="access2" value="Access" [checked]="full.checked"/>
        <label>Full</label>
      </td>
    </tr>
  </tbody>
</table>

And here is a link to a sample StackBlitz.

I am not sure why, for instance, all of the 'None' radio buttons are not checked when setting the [checked] as follow:

<input type="radio" name="access1" value="None" [checked]="none.checked"/>

Solution

  • Check for changes:

    <table (click)="cd.detectChanges()">
        <thead>
            <th>Role</th>
            <th colspan="3">Access</th>
        </thead>
        <tbody>
            <tr>
                <td>Select All</td>
                <td>
                    <input #none type="radio" name="access0" value="allNone"/>
            <label>None</label>
          </td>
          <td>
            <input #readOnly type="radio" name="access0" value="allReadOnly"/>
            <label>Read Only</label>
          </td>
          <td>
            <input #full type="radio" name="access0" value="AllFull"/>
            <label>Full</label>
          </td>
        </tr>
        <tr>
          <td>Admin</td>
          <td>
            <input type="radio" name="access1" [value]="None" [checked]="none.checked"/>
            <label>None</label>
          </td>
          <td>
            <input type="radio" name="access1" value="ReadOnly" [checked]="readOnly.checked"/>
            <label>Read Only</label>
          </td>
          <td>
            <input type="radio" name="access1" value="Access" [checked]="full.checked"/>
            <label>Full</label>
          </td>
        </tr>
        <tr>
          <td>Sales Person</td>
          <td>
            <input type="radio" name="access2" value="None" [checked]="none.checked"/>
            <label>None</label>
          </td>
          <td>
            <input type="radio" name="access2" value="ReadOnly" [checked]="readOnly.checked"/>
            <label>Read Only</label>
          </td>
          <td>
            <input type="radio" name="access2" value="Access" [checked]="full.checked"/>
            <label>Full</label>
          </td>
        </tr>
      </tbody>
    </table>
    

    And TS:

    import {ChangeDetectorRef, Component} from '@angular/core';
    
    @Component({
      selector: 'my-app',
      templateUrl: './app.component.html',
      styleUrls: [ './app.component.css' ]
    })
    export class AppComponent  {
      constructor(protected cd: ChangeDetectorRef){}
    }