|
7 | 7 | EventEmitter,
|
8 | 8 | HostBinding,
|
9 | 9 | Input,
|
| 10 | + NgZone, |
10 | 11 | OnInit,
|
11 | 12 | Output,
|
12 | 13 | QueryList,
|
@@ -189,6 +190,13 @@ export class SwiperComponent implements OnInit {
|
189 | 190 | return this._virtual;
|
190 | 191 | }
|
191 | 192 | private _virtual: SwiperOptions['virtual'];
|
| 193 | + |
| 194 | + @Input() |
| 195 | + set index(index: number) { |
| 196 | + if (index) { |
| 197 | + this.setIndex(index); |
| 198 | + } |
| 199 | + } |
192 | 200 | // prettier-ignore
|
193 | 201 | @Output('_beforeBreakpoint') s__beforeBreakpoint: EventEmitter<SwiperEvents['_beforeBreakpoint']> = new EventEmitter<any>();
|
194 | 202 | // prettier-ignore
|
@@ -342,6 +350,8 @@ export class SwiperComponent implements OnInit {
|
342 | 350 | // prettier-ignore
|
343 | 351 | @Output('swiper') s_swiper: EventEmitter<any> = new EventEmitter<any>();
|
344 | 352 |
|
| 353 | + @Output() indexChange = new EventEmitter<number>(); |
| 354 | + |
345 | 355 | @ViewChild('prevElRef', { static: false })
|
346 | 356 | set prevElRef(el: ElementRef) {
|
347 | 357 | this._setElement(el, this.navigation, 'navigation', 'prevEl');
|
@@ -389,7 +399,11 @@ export class SwiperComponent implements OnInit {
|
389 | 399 | }
|
390 | 400 |
|
391 | 401 | @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 | + ) {} |
393 | 407 |
|
394 | 408 | private _setElement(el: ElementRef, ref: any, update: string, key = 'el') {
|
395 | 409 | if (!el && !ref) {
|
@@ -428,6 +442,11 @@ export class SwiperComponent implements OnInit {
|
428 | 442 | };
|
429 | 443 |
|
430 | 444 | Object.assign(swiperParams.on, {
|
| 445 | + slideChange() { |
| 446 | + if (this.swiperRef) { |
| 447 | + this.indexChange.emit(this.swiperRef.realIndex); |
| 448 | + } |
| 449 | + }, |
431 | 450 | _containerClasses(swiper, classes) {
|
432 | 451 | this.containerClasses = classes;
|
433 | 452 | },
|
@@ -646,6 +665,20 @@ export class SwiperComponent implements OnInit {
|
646 | 665 | }
|
647 | 666 | }
|
648 | 667 |
|
| 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 | + |
649 | 682 | ngOnDestroy() {
|
650 | 683 | this.swiperRef.destroy();
|
651 | 684 | }
|
|
0 commit comments