activeIndex() in slides Not working

Ionic version: 2.x

Current behavior:

using slider.getActiveIndex() gives this error:

Error in ./Images class Images - caused by: Cannot read property ‘activeIndex’ of undefined

Expected behavior:

Without this bug i’ll be able to get the activeIndex of the active slide out of many different slides available to us in the array.

Related code:

in html file
<ion-slides #mySlider [options]="mySlideOptions" (ionDidChange)="onSlideChange()" >
        <ion-slide *ngFor="let obj1 of objectList">
            <img src="{{obj1.path}}">
            <p>{{ "HOME.ID" | translate }}</p >
        </ion-slide>
    </ion-slides> 

in ts file

@ViewChild('mySlider') slider:  Slides;

onSlideChanged() {
       this.currentIndex = this.slider.getActiveIndex() - 1;
        if (this.currentIndex < 0)
            this.currentIndex += 10;
        this.objectIndex = this.currentIndex;
    }

Error in ./Images class Images - caused by: Cannot read property 'activeIndex' of undefined Another error is: EXCEPTION: Cannot read property 'update' of undefined

i aslo posted this on ionic git issues #9837.

Guard against the case where the slider is not fully constructed yet:

onSlideChanged(): void {
  if (this.slider.slider) {
    ...
  }
}

It’s no longer(ionDidChange), it’s ionSlideDidChange as you can see from the docs here: https://ionicframework.com/docs/v2/api/components/slides/Slides/#output-events

A pull request was also opened with a similar issue already back in June here https://github.com/driftyco/ionic/issues/6762

Since you only list your Ionic version as 2.x it’s hard to know which version you have, but can choose your version in the documents section.

1 Like

Also you should rename onSlideChange() to onSlideChanged()

1 Like