使用 Angular 和 ngu-carousel 动态生成轮播幻灯片
Dynamically generated carousel slides using Angular and ngu-carousel
我正在尝试使用 ngu-carousel Angular 模块创建轮播,但我在设置时遇到了问题,轮播幻灯片是从一组对象动态生成的。根据我在使用中看到的这个模块的示例,每张幻灯片都必须在容器组件内部设置一个 <ng-template #first>
,该容器组件使用 [dataSource]
--> <ngu-carousel [dataSource]="[first, second, third]">
.'
但是,我似乎无法弄清楚如何设置唯一生成的本地引用,以便我可以使用 ngFor
创建幻灯片,如下所示:
<ng-template ngFor let-item [ngForOf]="items" let-i="index" #dynamicReferenceNeededHere>
<p>{{item.title}}</p>
</ng-template>
非常感谢任何帮助。
现在,您的旋转木马是动态的,它从 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>
我正在尝试使用 ngu-carousel Angular 模块创建轮播,但我在设置时遇到了问题,轮播幻灯片是从一组对象动态生成的。根据我在使用中看到的这个模块的示例,每张幻灯片都必须在容器组件内部设置一个 <ng-template #first>
,该容器组件使用 [dataSource]
--> <ngu-carousel [dataSource]="[first, second, third]">
.'
但是,我似乎无法弄清楚如何设置唯一生成的本地引用,以便我可以使用 ngFor
创建幻灯片,如下所示:
<ng-template ngFor let-item [ngForOf]="items" let-i="index" #dynamicReferenceNeededHere>
<p>{{item.title}}</p>
</ng-template>
非常感谢任何帮助。
现在,您的旋转木马是动态的,它从 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>