Angular:如何使用@ ngu / carousel库动态启动轮播

时间:2019-06-13 08:35:54

标签: angular angular6 carousel ngu-carousel

我要求启动轮播以动态索引而不是从0开始。我们是否有npm软件包,支持Angular 4或更高版本。

@ ngu / carousel已经被使用,myCarousel.moveTo()似乎不适用于该senario

2 个答案:

答案 0 :(得分:0)

您可以将主数组(带有图像的数组)随机化,这样页面的每个新视图在轮播中都会看到不同的顺序

相关的 HTML

<ngu-carousel #myCarousel [inputs]="carouselConfig" [dataSource]="carouselItems">
    <div *nguCarouselDef="let item;" class="item">
        <div class="tile">{{item}}</div>
    </div>
    <button NguCarouselNext class="rightRs">></button>
    <button NguCarouselPrev class="leftRs"><</button>
    <ul class="myPoint" NguCarouselPoint>
        <li *ngFor="let j of myCarousel.pointNumbers; let j = index" [class.active]="j==myCarousel.activePoint" (click)="myCarousel.moveTo(j)"></li>
    </ul>
</ngu-carousel>

相关的 TS

import { Component, AfterViewInit, ViewChild, ChangeDetectorRef } from '@angular/core';
import { NguCarousel, NguCarouselConfig } from '@ngu/carousel';

@Component({
  selector: 'app-simple',
  templateUrl: './simple.component.html',
  styleUrls: ['./simple.component.css']
})
export class SimpleComponent implements AfterViewInit {
  name = 'Angular';
  slideNo = 0;
  withAnim = true;
  resetAnim = true;

  @ViewChild('myCarousel') myCarousel: NguCarousel;
  carouselConfig: NguCarouselConfig = {
    grid: { xs: 1, sm: 1, md: 1, lg: 1, all: 0 },
    load: 3,
    interval: { timing: 4000, initialDelay: 1000 },
    loop: true,
    touch: true,
    velocity: 0.2
  }
  carouselItems: any[];

  constructor(private cdr: ChangeDetectorRef) {
    let myArray: any[] = ['item 1', 'item 2', 'item 3'];
    myArray = this.shuffleArray(myArray);
    this.carouselItems = myArray;
  }

  shuffleArray(array) {
    for (let i = array.length - 1; i > 0; i--) {
      const j = Math.floor(Math.random() * (i + 1));
      [array[i], array[j]] = [array[j], array[i]];
    }
    return array;
  }

  ngAfterViewInit() {
    this.cdr.detectChanges();
  }

  reset() {
    this.myCarousel.reset(!this.resetAnim);
  }

  moveTo(slide) {
    this.myCarousel.moveTo(slide, !this.withAnim);
  }
}

检查working stackblitz here

答案 1 :(得分:0)

myCarousel.moveTo()移到ngAfterViewInit(){}内而不是ngOnInit(){}对我有用。

响应旧线程,因为我以与先前答案不同的方式克服了相同的问题。