I'm using swiper library. In API from above link you can find that mySwiper.activeIndex
is returning currectly selected slide. What I want to do is update my service whenever slide is changed so my app will know which slide is selected in other parts (by sharing service).
So what I wanted to use is ngOnChanges
which suppose to detect every change in component but it doesn't detect slide change because my code doesn't console.log
anything. Code:
import { Component, OnInit, AfterViewInit, OnChanges } from '@angular/core';
import Swiper from 'swiper';
import {TouchService} from "../../services/touch.service";
@Component({
selector: 'app-nav',
templateUrl: './nav.component.html',
styleUrls: [ './nav.component.scss' ]
})
export class NavComponent implements AfterViewInit, OnChanges, OnInit {
mySwiper: Swiper;
slides = [
'Slide1',
'Slide2',
'Slide3',
'Slide4',
'Slide5',
'Slide6'
];
constructor(private touchService: TouchService) {}
ngAfterViewInit() {
this.mySwiper = new Swiper('.nav', {
paginationClickable: false,
grabCursor: true,
loop: true,
slidesPerView: 3,
spaceBetween: 50
});
this.mySwiper.on('touchStart', () => {
this.touchService.triggerTouchStart();
});
this.mySwiper.on('touchEnd', () => {
this.touchService.triggerTouchStop();
});
}
ngOnChanges(changes) {
console.log(changes);
}
ngOnInit() {
setTimeout(() => {
alert(this.mySwiper.activeIndex);
}, 4000);
}
}
In this code setTimeout works correctly and show me currently selected slide after 4 seconds from app load. But this ngOnChanges(changes) {
seems to be never fired because I don't see any console.logs.
Why ngOnChanges doesn't catch mySwiper
changes? What can I do to detect such changes?