Angular 6 - bootstrap轮播

时间:2018-06-13 17:27:41

标签: angular html5 bootstrap-4

我用bootstrap4创建轮播,图像来自阵列。我的问题是第一个元素必须有类active

<div id="carouselExampleFade" class="carousel slide carousel-fade" data-ride="carousel">
  <div class="carousel-inner">
    <div class="carousel-item" *ngFor="let image of sliderImages" [ngClass]="image[0] ? 'active' : null">
      <img class="d-block w-100" [src]="image" alt="First slide">
    </div>
  </div>
  <a class="carousel-control-prev" href="#carouselExampleFade" role="button" data-slide="prev">
    <span class="carousel-control-prev-icon" aria-hidden="true"></span>
    <span class="sr-only">Previous</span>
  </a>
  <a class="carousel-control-next" href="#carouselExampleFade" role="button" data-slide="next">
    <span class="carousel-control-next-icon" aria-hidden="true"></span>
    <span class="sr-only">Next</span>
  </a>
</div>

1 个答案:

答案 0 :(得分:4)

将以下内容添加到您的代码中,以便您键入ngFor对象的索引。

<div class="carousel-item" *ngFor="let image of sliderImages let i = index" [ngClass]="{'active' : i == 0}">

  <img class="d-block w-100" [src]="image" alt="First slide">

</div>

现在你可以键入i来确定循环的第一次迭代。