Angular 2 アプリに owl-carousel を実装しようとしています。
私はこの例に従いました Angular2でフクロウカルーセルを使用する方法? 実際には、アイテムが非同期変更 (ng-content async change) であるという唯一の問題でうまく機能します。
owl-courosel の内容が変更された (推奨者または批判者) ときに 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>
どんな助けでも本当に感謝しています。ありがとうございました。