使用Angular和ngu-carousel动态生成的轮播幻灯片

时间:2019-03-22 19:51:53

标签: angular

我正在尝试使用ngu-carousel Angular模块创建轮播,但是我无法对其进行设置,因此轮播幻灯片是由一系列对象动态生成的。根据我所看到的有关使用此模块的示例,每个幻灯片必须在容器组件内部设置<ng-template #first>,容器组件必须使用[dataSource]-> {{1 }}。'

但是,我似乎无法弄清楚如何设置要唯一生成的本地引用,以便可以使用<ngu-carousel [dataSource]="[first, second, third]">创建幻灯片,如下所示:

ngFor

任何帮助将不胜感激。

StackBlitz Example (see "static" component and templates)

1 个答案:

答案 0 :(得分:1)

现在,您的轮播是动态的,它从static.component.ts中的items数组中读取项目数;我在您的数组中添加了另一个字段,以显示在动态轮播中使用任意数量的字段的可能性;

使用您的existing stackblitz并按照以下代码更改2个文件的内容:

将您的 static.component.ts 设置为:

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

@Component({
  selector: 'app-static',
  templateUrl: './static.component.html',
  styleUrls: ['./static.component.css']
})

export class StaticComponent {
  name = 'Angular';
  slideNo = 1;
  withAnim = true;
  resetAnim = true; 

  @ViewChild('myCarousel') myCarousel: NguCarousel;

  @ViewChildren('linkRef') linkRefs;

  carouselConfig: NguCarouselConfig = {
    grid: { xs: 2, sm: 2, md: 2, lg: 2, all: 0 },
    load: 3,
    interval: {timing: 4000, initialDelay: 1000},
    loop: true,
    touch: true,
    velocity: 0.2
  }

  constructor(private cdr: ChangeDetectorRef) {}

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

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

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

  items = [
    { title: 'Slide One', state: 'small' ,subTitle:'sample sub title for 1' },
    { title: 'Slide Two', state: 'small' ,subTitle:'sample sub title for 2' },
    { title: 'Slide Three', state: 'small' ,subTitle:'sample sub title for 3' },
    { title: 'Slide Four', state: 'small' ,subTitle:'sample sub title for 4' },
    { title: 'Slide Five', state: 'small' ,subTitle:'sample sub title for 5' },
    { title: 'Slide Six', state: 'small' ,subTitle:'sample sub title for 6' }
  ];

}

将您的 static.component.html 设置为:

<ngu-carousel #myCarousel [inputs]="carouselConfig" [dataSource]="items">
    <div *nguCarouselDef="let item;" class="item">
        <div class="tile">
     <b>{{item.title}}</b> : 
     {{item.subTitle}}
        </div>
    </div>

<!-- this is the ng-teamplate I'd like to use for the carousel slides -->
<!--
<ng-template ngFor let-item [ngForOf]="items" let-i="index" #dynamicReferenceNeededHere>
   <p>{{item.title}}</p>
</ng-template> 
-->
<!--
    <ng-template #first>
        <p>First</p>
    </ng-template>

    <ng-template #second>
        <p>Second</p>
    </ng-template>

    <ng-template #third>
        <p>Third</p>
    </ng-template>
-->

    <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>