我正在嘗試在我的Angular 2應用中實現owl-carousel。如何在Angular2中使用owl-carousel以及異步改變內容
我跟着這個例子How to use owl-carousel in Angular2?,它實際上運行良好,唯一的問題是我的項目是異步變化(ng-content異步變化)。
當我的貓頭鷹的內容發生變化(啓動器或評估者)時,通過在plnkr上實現解決方案,插件不會重新加載。所以我只看到一個項目列表,但它們不滾動。
所以我有NPS-comments.component.html在轉盤組件稱爲:
<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>
那麼實際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;
}
}
這是carousel.component.html:
<div class="owl-carousel owl-theme">
<ng-content></ng-content>
</div>
任何幫助將非常感激。 謝謝。
請提供一些代碼,演示了嘗試什麼,什麼不起作用如預期。 –
你提到你正在用一個plnkr來實現另一個例子,你可以鏈接那個plnkr,這樣我們都可以看看它並嘗試一些事情嗎? –
我放了一些代碼,希望它有幫助。將嘗試儘快產生一個plnkr。謝謝你的幫助。 – CristyTarantino