Set slider index programatically

Hi, I have a page which contains a slider. This page receives as a parameter of its constructor the index of the initial page of the slide to show. Although it is easy to set initialSlide of the slider at coding time, I can’t figure out how to set it at run time. Here is what I have so far, which of course does not work:

export class ClientOverviewPage {
  @ViewChild('mySlider') slider: Slides;
  private currentIndex: number;
  sliderOptions = {
    initialSlide: this.currentIndex
  }

  constructor(private navCtrl: NavController, navParams: NavParams) {
    this.currentIndex = navParams.get('index');
  }
}
1 Like

I think what you are looking for is

ionViewDidEnter() {
  
}

Hum, I gave that a shot but although ionViewDidEnter gets called, changing the slide does not seem to work.It always starts on the first slide. I’m on beta 10.

  private ionViewDidEnter() {
    this.slider.update();
    this.slider.slideTo(this.currentIndex);
  }

Got it to work changing the code to

  private ionViewDidEnter() {
    this.slider.getSlider().update();
    this.slider.slideTo(this.currentIndex, 0);
  }

I would have thought that this.slider.update() and this.slider.getSlider().update() to be equivalent… :confused:

I haven’t fooled around with the Slider component at all yet so I can’t really help you there, but ionViewDidEnter is your go to method to run any code after the current view has fully rendered.

1 Like

Thanks,at least you’ve sent me in the right direction. More googling led me to the complete solution.

1 Like

This worked for me too,

  ionViewDidEnter(){
    this.slider.slideTo(2, 500);
  }

Thank you

Thanks.
This solution is working fine for me.
But one issue is here

when I enter into view, it shows me first slide and change it after 1 or 2 second.

Please suggest me solution if any??

As you suggested to use ionViewDidEnter(), i used it but it was quite slow.
but then i used ionViewWillEnter(), it works fine

in ionic 4 just mention index like 0,1 where you wanna go