My app is rendering fine, but why would the change
event function updateItem(item)
not be firing?
Template:
<ion-list>
<ion-item-sliding *ngFor="#item of items">
<ion-item>
<ion-label>{{item.title}}</ion-label>
<ion-toggle [(ngModel)]="item.completed" (change)="updateItem(item)"></ion-toggle>
</ion-item>
<ion-item-options>
<button primary (click)="editItem(item)">
<ion-icon name="edit"></ion-icon>Edit
</button>
<button secondary (click)="deleteItem(item)">
<ion-icon name="delete"></ion-icon>Delete
</button>
</ion-item-options>
</ion-item-sliding>
</ion-list>
Class:
export class Todos {
...
updateItem(item) {
alert(1)
this._todosService.update(item).subscribe(
response => { this.getItems(); }
);
}
...
}
On further testing, this is not a satisfactory answer as it calls the change event when the toggles are being rendered for the first time. This is not the desired result. It appears that the (change) event not firing is actually a bug in the latest beta of Ionic 2.
It appears that the (change) event not firing is actually a bug in the latest beta of Ionic 2. If initial value true then ionChange is not useful because its is trigger every time.
If you set toogle: as false then the event (change) will be not triggered when the app starts, but as true will be triggered, that's not make sense. I use (ionChange) but same effect. This event must be triggered only when the state of toggle changes.
The value of a toggle is analogous to the value of a <input type="checkbox"> , it's only used when the toggle participates in a native <form>. Emitted when the toggle loses focus.
According to the Ionic documentation, you can do that :
<ion-toggle [(ngModel)]="item.completed" (ionChange)="updateItem(item)" checked="false"></ion-toggle>
I hope this will help you!
update
<ion-toggle [(ngModel)]="itemCompleted"></ion-toggle>
export class Todos {
get itemCompleted() {
return item.completed;
}
set itemCompleted(value) {
item.completed = value;
updateItem(item);
}
...
updateItem(item) {
alert(1)
this._todosService.update(item).subscribe(
response => { this.getItems(); }
);
}
...
}
original
When [(ngModel)]="..."
works, this
(ngModelChange)="updateItem(item)"
should work as well and probably do what you try to accomplish.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With