I have a modal containing an ion-slides element. In the main page I have the thumbnails of the images that on (click)
open the modal with the slider. I need to have the slider slideTo
the slide with the index of the thumbnail selected, and haven't gotten it working yet.
I have accomplished passing the right index to the modal and verifying it logging to the console. In the ionic 4 docs, we are directed to the swiper docs, where one can see the slideTo
method exists, but I just don't know how to use it when the slider was initialized by ionic. I followed a tutorial to get the modal and slider going and everything works great except for opening the slider at the right slide. I have tried in many ways to follow the swiper docs. Also, the array of thumbnails and the array of images for the slider are identical, so the indexes match.
Here the ts file for the modal/slider:
import { Component, OnInit, ViewChild, ElementRef } from '@angular/core';
import { NavParams, ModalController } from '@ionic/angular';
@Component({
selector: 'app-zoom-modal',
templateUrl: './zoom-modal.component.html',
styleUrls: ['./zoom-modal.component.scss'],
})
export class ZoomModalComponent implements OnInit {
images: any;
index: number;
img: any;
@ViewChild('slider', { static: false })slider: ElementRef;
sliderOpts = {
zoom: {
maxRatio: 3,
},
slideTo: this.index // doesn't work
};
constructor(private navParams: NavParams, private modalController: ModalController) { }
ngOnInit() {
console.log(this.index);
}
zoom(zoomIn: boolean){
let zoom = this.slider.nativeElement.swiper.zoom;
if (zoomIn) {
zoom.in();
} else {
zoom.out();
}
}
close() {
this.modalController.dismiss();
}
slideLoad(){
let modelslider = this.slider.nativeElement.swiper;
modelslider.slideTo(this.index); // doesn't work
}
}
Screenshot of the method in the docs:
Template:
<ion-content fullscreen>
<ion-slides [options]="sliderOpts" (ionSlidesDidLoad)="slideLoad()" (ionSlideDidChange)="slideChange()" #slider>
<ion-slide *ngFor="let img of images; let i = index">
<div class="swiper-zoom-container">
<img src="{{ img.url }}" />
</div>
</ion-slide>
</ion-slides>
<ion-toolbar fixed>
<div id="modal-button-wrap">
<ion-button (click)="close()" id="modal-close" class="clear">
<ion-icon name="close"></ion-icon>
</ion-button>
<ion-button (click)="delete()" id="modal-edit">
<ion-icon name="create"></ion-icon>
<ion-label> EDIT PHOTO</ion-label>
</ion-button>
<ion-button (click)="delete()" id="modal-delete" class="clear">
<ion-icon name="trash"></ion-icon>
</ion-button>
</div>
</ion-toolbar>
</ion-content>