mercredi 19 décembre 2018

How does one read checkboxes created in an Angular 6 template

I have some checkboxes being created this way:

        <table>
  <tr class='collectionList' *ngFor="let option of configurationTemplate">
    <td class='formLabel'></td>
    <td>
    <ng-container *ngIf="option.type == 'boolean'">
      <input class='configurationForm' id='id-' type='checkbox' name=""  [checked]="option.value"  [(ngModel)]="option.value"   />
    </ng-container>
    </td>
  </tr>
  </table>

I when I click a button, I fire a method in my controller that does this:

var elements = document.getElementsByClassName('configurationForm');
    var obj = {};
    for(var i=0; i<elements.length; i++) {
      var element = elements[i];
      var name = element.id.replace("id-", "");
      if(element.className.indexOf('dirty')>-1) {
        //console.log(element.id);
        obj[name] = 1;
      } else {
        //console.log(element.id);
        obj[name] = 0;
      }

    }

The problem is that there is no property in the elements I am looping through to tell me whether or not they are checked. So I ended up looking at the className (as you see) -- but this doesn't work when the checkboxes haven't been changed. I just want a checked property to determine whether or not the checkbox is checked, but Angular doesn't seem to provide one. Any ideas what I am doing wrong? This stuff is super easy with plain (no-framework) Javascript.




Aucun commentaire:

Enregistrer un commentaire