Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ionic2 toggle component change event not firing

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(); }
    );
  }
... 
}
like image 928
Dave Avatar asked Feb 25 '16 13:02

Dave


People also ask

Why is the (change) event not firing in ionic?

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.

Why ionchange is not working in ion2?

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.

What is the difference between (ionchange) and (toogle)?

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.

What does the <input type> value of a toggle mean?

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.


2 Answers

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!

like image 185
Fryck Avatar answered Oct 18 '22 20:10

Fryck


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.

like image 26
Günter Zöchbauer Avatar answered Oct 18 '22 22:10

Günter Zöchbauer