Sebastian
Sebastian

Reputation: 3

Using variable of ngfor in iondidchange - ionic 2

Iam learning ionic 2 and actually I got the following situation:

I have a ngfor-loop within ion-slides. I want to call a function after a slide changed, so I used the (ionDidChange) attribute inside the ion-slides. My issue is, that I need a local variable of the current slide in my ngfor-loop during this function call, which is not inside that loop and I have no Idea how to handle that.

Here is my Code:

<ion-slides effect='slide' [options]="mySlideOptions" #mySlider (ionDidChange)=slideChanged(stuff.id)>
  <ion-slide *ngFor="let stuff of stuffs">
    {{stuff.id}}
  </ion-slide>
</ion-slides>

Of course I get an error: "Cannot read property 'id' of undefined", because of the stuff.id in the function call, but I want to show you what I am looking for.

I am sure there has to be a very simple solution, but I dont get it. Thank you.

Upvotes: 0

Views: 931

Answers (1)

Suraj Rao
Suraj Rao

Reputation: 29614

you will have to associate the slide index with your object list index.You can get previous and active slide from slides also the event is ionSlideDidChange.

<ion-slides effect='slide' [options]="mySlideOptions" #mySlider (ionSlideDidChange)=slideChanged()>
  <ion-slide *ngFor="let stuff of stuffs">
    {{stuff.id}}
  </ion-slide>
</ion-slides>

In the Component:

slideChanged(){
let index = this.slides.getPreviousIndex();
let requiredId = this.stuffs[index].id;
}

Upvotes: 1

Related Questions