samedi 26 mai 2018

Angular 2: Array with checkbox not working properly

I'm binding one array with Checkbox. However, When I try to change bool attribute for single element of array but it changes for all elements.

My HTML Component is as below.

<div class="col-sm-4" *ngFor="let karyalay of karyalayListFinal">
  <div class="checkbox-fade fade-in-primary">
    <label>
      <input formControlName="karyalay_group" type="checkbox" name="karyalaysCheckbox" value="" [(ngModel)]="karyalay.isChecked" 
      (click)="callEvents(karyalay.karyalayId)">
      <span></span>
    </label>
  </div>
</div>

Now I'm trying to change value of single or selected element as below.

for (let karyalay of this.karyalayListFinal) {
    let tempInd = _.findIndex(this.roleMasterEventList, {'KARYALAY_ID': karyalay.karyalayId});
    if (tempInd > -1) {
      this.karyalayListFinal[tempInd].isChecked = true;
    }
}

Actually, if tempInd > -1 then and then only that element's value should be changed. But it changes for all.

Don't know whether this is ngModel issue or what?

Thanks




Aucun commentaire:

Enregistrer un commentaire