jeudi 5 mai 2016

Bind click and change checkbox angular2

I have a strange problem with checkboxes using angular2 (beta 17).

I'm trying to set same checkboxes as elements has the array and bind the change or click event.

For example I have an array:

objects=[
    {value:true},
    {value:true},
    {value:false},
    {value:true}
];

And I'm using the ngFor to create all the checkboxes:

<div *ngFor="let object of objects; let i = index" >
    <input 
            type="checkbox" 
            [checked]="object.value" 
            (change)="updateCheckedOptions(i, $event)"
    />
</div>

And finally I have a function in my component:

updateCheckedOptions(index,event){
    this.objects[index].value=event.target.checked;
}

But the problem is that the checkbox don't change, I mean that when I click the checkbox, if the initial state is checked, always remain checked and vice versa.

I have tried using (click), (change), [(ngModel)] but it does not work any.

Thanks!




Aucun commentaire:

Enregistrer un commentaire