@@ -392,9 +392,11 @@ export class SwiperComponent implements OnInit {
392
392
set paginationElRef ( el : ElementRef ) {
393
393
this . _setElement ( el , this . pagination , 'pagination' ) ;
394
394
}
395
+ @ViewChild ( 'wrapperEl' , { static : false } ) wrapperEl : ElementRef ;
395
396
@ContentChildren ( SwiperSlideDirective , { descendants : true , emitDistinctChangesOnly : true } )
396
397
slidesEl : QueryList < SwiperSlideDirective > ;
397
398
private slides : SwiperSlideDirective [ ] ;
399
+ slidesObserver : MutationObserver ;
398
400
399
401
prependSlides : Observable < SwiperSlideDirective [ ] > ;
400
402
appendSlides : Observable < SwiperSlideDirective [ ] > ;
@@ -441,6 +443,8 @@ export class SwiperComponent implements OnInit {
441
443
442
444
ngAfterViewInit ( ) {
443
445
this . childrenSlidesInit ( ) ;
446
+ this . observeSlidesChanges ( ) ;
447
+
444
448
if ( this . init ) {
445
449
this . initSwiper ( ) ;
446
450
this . _changeDetectorRef . detectChanges ( ) ;
@@ -465,15 +469,29 @@ export class SwiperComponent implements OnInit {
465
469
this . prependSlides = of ( this . slides . slice ( this . slides . length - this . loopedSlides ) ) ;
466
470
this . appendSlides = of ( this . slides . slice ( 0 , this . loopedSlides ) ) ;
467
471
}
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 ) {
472
484
this . swiperRef . update ( ) ;
473
- } ) ;
485
+ }
474
486
} ) ;
475
- }
476
- } ;
487
+
488
+ this . slidesObserver . observe ( wrapperEl , {
489
+ attributes : false ,
490
+ childList : true ,
491
+ characterData : false ,
492
+ } ) ;
493
+ } ) ;
494
+ }
477
495
478
496
get isSwiperActive ( ) {
479
497
return this . swiperRef && ! this . swiperRef . destroyed ;
@@ -756,5 +774,6 @@ export class SwiperComponent implements OnInit {
756
774
757
775
ngOnDestroy ( ) {
758
776
this . swiperRef ?. destroy ( ) ;
777
+ this . slidesObserver ?. disconnect ( ) ;
759
778
}
760
779
}
0 commit comments