Skip to content

Commit 962a0c0

Browse files
committed
feat(angular): use observer to update swiper on slides changes
1 parent fef6ebd commit 962a0c0

File tree

2 files changed

+27
-8
lines changed

2 files changed

+27
-8
lines changed

src/angular/src/swiper.component.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
</ng-container>
66
<div *ngIf="scrollbar && showScrollbar" class="swiper-scrollbar" #scrollbarElRef></div>
77
<div *ngIf="pagination && showPagination" class="swiper-pagination" #paginationElRef></div>
8-
<div [ngClass]="wrapperClass">
8+
<div [ngClass]="wrapperClass" #wrapperEl>
99
<ng-content select="[slot=wrapper-start]"></ng-content>
1010
<ng-template
1111
*ngTemplateOutlet="

src/angular/src/swiper.component.ts

+26-7
Original file line numberDiff line numberDiff line change
@@ -392,9 +392,11 @@ export class SwiperComponent implements OnInit {
392392
set paginationElRef(el: ElementRef) {
393393
this._setElement(el, this.pagination, 'pagination');
394394
}
395+
@ViewChild('wrapperEl', { static: false }) wrapperEl: ElementRef;
395396
@ContentChildren(SwiperSlideDirective, { descendants: true, emitDistinctChangesOnly: true })
396397
slidesEl: QueryList<SwiperSlideDirective>;
397398
private slides: SwiperSlideDirective[];
399+
slidesObserver: MutationObserver;
398400

399401
prependSlides: Observable<SwiperSlideDirective[]>;
400402
appendSlides: Observable<SwiperSlideDirective[]>;
@@ -441,6 +443,8 @@ export class SwiperComponent implements OnInit {
441443

442444
ngAfterViewInit() {
443445
this.childrenSlidesInit();
446+
this.observeSlidesChanges();
447+
444448
if (this.init) {
445449
this.initSwiper();
446450
this._changeDetectorRef.detectChanges();
@@ -465,15 +469,29 @@ export class SwiperComponent implements OnInit {
465469
this.prependSlides = of(this.slides.slice(this.slides.length - this.loopedSlides));
466470
this.appendSlides = of(this.slides.slice(0, this.loopedSlides));
467471
}
468-
this._changeDetectorRef.markForCheck();
469-
if (this.swiperRef) {
470-
this.zone.runOutsideAngular(() => {
471-
setTimeout(() => {
472+
this._changeDetectorRef.detectChanges();
473+
console.log('? update');
474+
};
475+
476+
private observeSlidesChanges() {
477+
this.zone.runOutsideAngular(() => {
478+
const wrapperEl = this.wrapperEl?.nativeElement;
479+
if (!wrapperEl) {
480+
return;
481+
}
482+
this.slidesObserver = new MutationObserver((mutations) => {
483+
if (this.swiperRef) {
472484
this.swiperRef.update();
473-
});
485+
}
474486
});
475-
}
476-
};
487+
488+
this.slidesObserver.observe(wrapperEl, {
489+
attributes: false,
490+
childList: true,
491+
characterData: false,
492+
});
493+
});
494+
}
477495

478496
get isSwiperActive() {
479497
return this.swiperRef && !this.swiperRef.destroyed;
@@ -756,5 +774,6 @@ export class SwiperComponent implements OnInit {
756774

757775
ngOnDestroy() {
758776
this.swiperRef?.destroy();
777+
this.slidesObserver?.disconnect();
759778
}
760779
}

0 commit comments

Comments
 (0)