Skip to content

Commit 86670bd

Browse files
committed
feat(angular): index control
1 parent e66c241 commit 86670bd

File tree

3 files changed

+41
-1
lines changed

3 files changed

+41
-1
lines changed

playground-angular/src/app/app.component.html

+6
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
[breakpoints]="breakpoints"
2525
[scrollbar]="scrollbar"
2626
[loop]="true"
27+
[(index)]="indexNumber"
2728
[pagination]="{ type: 'fraction' }"
2829
>
2930
<ng-template swiperSlide>Best slide ever 1</ng-template>
@@ -39,4 +40,9 @@
3940
<button (click)="toggleNavigation()">Toggle navigation</button>
4041
<button (click)="toggleScrollbar()">Toggle scrollbar</button>
4142
<button (click)="breakpointChange()">Breakpoint change</button>
43+
44+
<button (click)="indexNumber = 0">0</button>
45+
<button (click)="indexNumber = 3">3</button>
46+
<button (click)="indexNumber = 5">5</button>
47+
<button (click)="indexNumber = 6">6</button>
4248
</main>

playground-angular/src/app/app.component.ts

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ export class AppComponent {
1515
SwiperCore.use([Navigation, Pagination, Scrollbar, A11y, Virtual]);
1616
}
1717

18+
indexNumber = 1;
1819
slidesPerView: number = 4;
1920
pagination: any = false;
2021

src/angular/src/swiper.component.ts

+34-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
EventEmitter,
88
HostBinding,
99
Input,
10+
NgZone,
1011
OnInit,
1112
Output,
1213
QueryList,
@@ -189,6 +190,13 @@ export class SwiperComponent implements OnInit {
189190
return this._virtual;
190191
}
191192
private _virtual: SwiperOptions['virtual'];
193+
194+
@Input()
195+
set index(index: number) {
196+
if (index) {
197+
this.setIndex(index);
198+
}
199+
}
192200
// prettier-ignore
193201
@Output('_beforeBreakpoint') s__beforeBreakpoint: EventEmitter<SwiperEvents['_beforeBreakpoint']> = new EventEmitter<any>();
194202
// prettier-ignore
@@ -342,6 +350,8 @@ export class SwiperComponent implements OnInit {
342350
// prettier-ignore
343351
@Output('swiper') s_swiper: EventEmitter<any> = new EventEmitter<any>();
344352

353+
@Output() indexChange = new EventEmitter<number>();
354+
345355
@ViewChild('prevElRef', { static: false })
346356
set prevElRef(el: ElementRef) {
347357
this._setElement(el, this.navigation, 'navigation', 'prevEl');
@@ -389,7 +399,11 @@ export class SwiperComponent implements OnInit {
389399
}
390400

391401
@HostBinding('class') containerClasses = 'swiper-container';
392-
constructor(private elementRef: ElementRef, private _changeDetectorRef: ChangeDetectorRef) {}
402+
constructor(
403+
private zone: NgZone,
404+
private elementRef: ElementRef,
405+
private _changeDetectorRef: ChangeDetectorRef,
406+
) {}
393407

394408
private _setElement(el: ElementRef, ref: any, update: string, key = 'el') {
395409
if (!el && !ref) {
@@ -428,6 +442,11 @@ export class SwiperComponent implements OnInit {
428442
};
429443

430444
Object.assign(swiperParams.on, {
445+
slideChange() {
446+
if (this.swiperRef) {
447+
this.indexChange.emit(this.swiperRef.realIndex);
448+
}
449+
},
431450
_containerClasses(swiper, classes) {
432451
this.containerClasses = classes;
433452
},
@@ -646,6 +665,20 @@ export class SwiperComponent implements OnInit {
646665
}
647666
}
648667

668+
setIndex(index: number, speed?: number, silent?: boolean): void {
669+
if (!this.swiperRef) {
670+
this.initialSlide = index;
671+
return;
672+
}
673+
this.zone.runOutsideAngular(() => {
674+
if (this.loop) {
675+
this.swiperRef.slideToLoop(index, speed, !silent);
676+
} else {
677+
this.swiperRef.slideTo(index, speed, !silent);
678+
}
679+
});
680+
}
681+
649682
ngOnDestroy() {
650683
this.swiperRef.destroy();
651684
}

0 commit comments

Comments
 (0)