jeudi 13 juillet 2017

Angular 2: Default checked on checkbox in ngFor

I'm trying to set default value as checked on a checkbox inside my ngFor. This is my array of checkbox items:

  tags = [{
    name: 'Empathetic',
    checked: false
  }, {
    name: 'Smart money',
    checked: true
  }, {
    name: 'Minimal help after writing check',
    checked: false
  }, {
    name: 'Easy term sheet',
    checked: true
  }];

This is my html

    <div class="form-group">
      <div class="form-check" *ngFor="let tag of tags;">
        <label class="form-check-label" for="tag">
          <input
            class="form-check-input"
            type="checkbox"
            id="tag"
            name="tagOptions"
            [(ngModel)]="tag.checked">
          
        </label>
      </div>
   </div>

The desired result is to get 2 checked, and 2 unchecked boxes, but all of them are unchecked. I've also tried different variations with [checked]="tag.checked", but it didn't seem to do the trick.

Aucun commentaire:

Enregistrer un commentaire