I am trying to implement owl-carousel into my Angular 2 app.
I followed this example How to use owl-carousel in Angular2? and it actually works well with the only problem that my items are async change (ng-content async change).
By implementing the solution on plnkr when the content of my owl-courosel changes (promoter or detractor), the plugin doesn't reload. So I see just a list of the items but they do not scroll.
So I have nps-comments.component.html where the carousel component is called:
<section class="purchasers comments" *ngIf="comments.promoters.length || comments.detractors.length">
<carousel class="promoters" *ngIf="comments.promoters.length" [options]="{ items: 1 }">
<p *ngFor="let promoter of comments.promoters">{{promoter}}</p>
</carousel>
<carousel class="detractors" *ngIf="comments.detractors.length" [options]="{ items: 1 }">
<p *ngFor="let detractor of comments.detractors">{{detractor}}</p>
</carousel>
</section>
Then the actual carousel.component.ts
import {
Component,
Input,
ElementRef
} from '@angular/core';
import 'jquery';
import 'owl-carousel';
@Component({
moduleId: module.id,
selector: 'carousel',
templateUrl: 'carousel.component.html',
styleUrls: ['carousel.component.css']
})
export class CarouselComponent {
@Input() options: Object;
private $carouselElement: any;
private defaultOptions: Object = {};
constructor(private el: ElementRef) { }
ngAfterViewInit() {
for (let key in this.options) {
if (this.options.hasOwnProperty(key)) {
this.defaultOptions[key] = this.options[key];
}
}
let outerHtmlElement: any = $(this.el.nativeElement);
this.$carouselElement = outerHtmlElement.find('.owl-carousel').owlCarousel(this.defaultOptions);
}
ngOnDestroy() {
this.$carouselElement.trigger('destroy.owl.carousel');
this.$carouselElement = null;
}
}
And this is the carousel.component.html:
<div class="owl-carousel owl-theme">
<ng-content></ng-content>
</div>
Any help would be really appreciated. Thank you.