我在 Angular 7 中使用 ng-bootstrap 插件来创建轮播。以下是 HTML:
<ngb-carousel *ngIf="images" (slide)="onSlide($event)">
<ng-template ngbSlide *ngFor="let image of images">
<img [src]="image" alt="Random slide">
<div class="carousel-caption">
<h3>{{image.id}}</h3>
</div>
</ng-template>
</ngb-carousel>
我的组件中有 onSlide 函数:
onSlide(e){
if(/*Some condition*/){
//how to move to next image here?
}
}
正如评论中提到的,在 onSlide 函数中,我想根据某些条件移动到下一个图像。如何在那里的 if 条件下移动到下一个图像?
我参考了 ng-bootstarap 的官方文档。他们在那里提到了 next() 方法。但我确定如何使用那个。任何帮助都感激不尽。
从 中获取模板引用ngb-carousel
,并将其赋予onSlide
函数:
<ngb-carousel #caro *ngIf="images" (slide)="onSlide($event, caro)">
caro.next()
在特定条件下调用:
onSlide(e, caro){
if(/*Some condition*/){
caro.next()
}
}
小心它可能会陷入无限循环,因为next()
调用了一个(slide)
事件。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句