Skip to content

Commit

Permalink
feat(angular): use swipers observer
Browse files Browse the repository at this point in the history
  • Loading branch information
vltansky committed Feb 4, 2021
1 parent 81da2a1 commit 30dd7c9
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 23 deletions.
2 changes: 1 addition & 1 deletion src/angular/src/swiper.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
</ng-container>
<div *ngIf="scrollbar && showScrollbar" class="swiper-scrollbar" #scrollbarElRef></div>
<div *ngIf="pagination && showPagination" class="swiper-pagination" #paginationElRef></div>
<div [ngClass]="wrapperClass" #wrapperEl>
<div [ngClass]="wrapperClass">
<ng-content select="[slot=wrapper-start]"></ng-content>
<ng-template
*ngTemplateOutlet="
Expand Down
26 changes: 4 additions & 22 deletions src/angular/src/swiper.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,6 @@ export class SwiperComponent implements OnInit {
set paginationElRef(el: ElementRef) {
this._setElement(el, this.pagination, 'pagination');
}
@ViewChild('wrapperEl', { static: false }) wrapperEl: ElementRef;
@ContentChildren(SwiperSlideDirective, { descendants: true, emitDistinctChangesOnly: true })
slidesEl: QueryList<SwiperSlideDirective>;
private slides: SwiperSlideDirective[];
Expand Down Expand Up @@ -443,7 +442,7 @@ export class SwiperComponent implements OnInit {

ngAfterViewInit() {
this.childrenSlidesInit();
this.observeSlidesChanges();
// this.observeSlidesChanges();

if (this.init) {
this.initSwiper();
Expand Down Expand Up @@ -472,26 +471,6 @@ export class SwiperComponent implements OnInit {
this._changeDetectorRef.detectChanges();
};

private observeSlidesChanges() {
this.zone.runOutsideAngular(() => {
const wrapperEl = this.wrapperEl?.nativeElement;
if (!wrapperEl) {
return;
}
this.slidesObserver = new MutationObserver((mutations) => {
if (this.swiperRef) {
this.swiperRef.update();
}
});

this.slidesObserver.observe(wrapperEl, {
attributes: false,
childList: true,
characterData: false,
});
});
}

get isSwiperActive() {
return this.swiperRef && !this.swiperRef.destroyed;
}
Expand Down Expand Up @@ -555,6 +534,9 @@ export class SwiperComponent implements OnInit {
this._changeDetectorRef.detectChanges();
},
});

swiperParams.observer = true;
swiperParams.observeSlideChildren = true;
new Swiper(this.elementRef.nativeElement, swiperParams);
}

Expand Down

0 comments on commit 30dd7c9

Please sign in to comment.