If you want to perform the listener action from TS, we can use addEventListener
, with the name as swiperslidechange
, I am using AbortController
to remove the listener.
export class App {
abort: AbortController = new AbortController();
...
...
ngAfterViewInit() {
this.swiper.nativeElement.addEventListener(
'swiperslidechange',
(event: any) => {
const [swiper, progress] = event.detail;
console.log('from TS:', event);
}, {
signal: this.abort.signal,
}
);
}
...
...
ngOnDestroy() {
this.abort.abort();
}
}
If you want to listen to DOM events, you can listen for swiperslidechange
HTML event with angular event binding.
<swiper-container init="false" #swiper (swiperslidechange)="swiperslidechange($event)">
@for (projectImagePath of projectImagesPath; track projectImagePath) {
<swiper-slide>
<img [src]="projectImagePath" alt=""/>
</swiper-slide>
}
</swiper-container>
TS:
swiperslidechange(data: any) {
console.log('from DOM', data);
}
Full Code:
import {
Component,
CUSTOM_ELEMENTS_SCHEMA,
ElementRef,
ViewChild,
} from '@angular/core';
import { MatGridListModule } from '@angular/material/grid-list';
@Component({
selector: 'app-root',
standalone: true,
imports: [MatGridListModule],
template: `
<swiper-container init="false" #swiper (swiperslidechange)="swiperslidechange($event)">
@for (projectImagePath of projectImagesPath; track projectImagePath) {
<swiper-slide>
<img [src]="projectImagePath" alt=""/>
</swiper-slide>
}
</swiper-container>
`,
schemas: [CUSTOM_ELEMENTS_SCHEMA],
})
export class App {
abort: AbortController = new AbortController();
@ViewChild('swiper') swiper!: ElementRef<any>;
projectImagesPath = [
'https://placehold.co/600x400?1',
'https://placehold.co/600x400?2',
'https://placehold.co/600x400?3',
'https://placehold.co/600x400?4',
];
swiperslidechange(data: any) {
console.log('from DOM', data);
}
ngAfterViewInit() {
// swiper parameters
const swiperParams = {
slidesPerView: 1,
navigation: true,
loop: true,
pagination: { clickable: true },
};
Object.assign(this.swiper.nativeElement, swiperParams);
this.swiper.nativeElement.initialize();
this.swiper.nativeElement.addEventListener(
'swiperslidechange',
(event: any) => {
const [swiper, progress] = event.detail;
console.log('from TS:', event);
},
{
signal: this.abort.signal,
}
);
}
ngOnDestroy() {
this.abort.abort();
}
}