微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在 Angular 的 ngx owl carousel 中设置活动幻灯片

如何解决如何在 Angular 的 ngx owl carousel 中设置活动幻灯片

我正在尝试根据索引将幻灯片设置为活动幻灯片。我试图将课程设置为活跃和居中。以下示例中的 newsId 值来自我正在获取和验证的另一个页面

<owl-carousel-o [options]="newsOptions" class="slider-service">
    <ng-template carouselSlide [ngClass]="{'active center': news.newsId == newsId}" *ngFor="let news of newsSlides">
        .....
    </ng-template>
</owl-carousel-o>

有没有其他方法可以设置活动幻灯片

解决方法

您必须使用 owlCar.to(slideIndex:string) 函数。

为每张幻灯片设置 ID:

<owl-carousel-o [options]="newsOptions" class="slider-service">
    <ng-template carouselSlide *ngFor="let news of newsSlides" [id]="news.newsId">
        .....
    </ng-template>
</owl-carousel-o>

在你的组件中定义 owl carousel 变量并像这样设置活动幻灯片

@Component({
  selector: 'app-news',templateUrl: './news.component.html',styleUrls: ['./news.component.scss']
})
export class NewsComponent implements OnInit {

  @Input() newsId: string;
  @ViewChild('owlCar',{ static: false }) owlCar: any;

  ngOnInit(): void {
    this.owlCar.to(this.newsId);
  }
}

记住 newsId 和 news.id 必须是字符串。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。