【发布时间】:2021-06-07 14:08:36
【问题描述】:
我正在尝试使用角度材料创建简单的图像模式。 next() 应该是从数组中查看下一张图片,prev() 是查看上一张图片。
问题 next 和 prev 函数没有按预期工作。如果单击 next() 索引只增加 +1 然后停止。如果我点击 prev() 索引变为-1
app.ts
const imageArray = [
{
imageData: [
'https://via.placeholder.com/50',
'https://via.placeholder.com/60'
],
},
{
imageData: [
'https://via.placeholder.com/100'
],
},
{
imageData: [
'https://via.placeholder.com/150'
],
}
];
next() {
this.currentImage = this.imageCollection[this.imageIndex + 1];
}
prev() {
this.currentImage = this.imageCollection[this.imageIndex - 1];
}
processImage() {
const rawData = this.imageArray;
this.imageCollection = rawData.flatMap((el: any) => el.imageData);
this.imageIndex = this.imageCollection.findIndex((x) => x === this.data.selectedImage);
this.currentImage = this.imageCollection[this.imageIndex];
}
app.html
<img [src]="currentImage"
alt="customer document" />
<div (click)="next()">next</div>
<div (click)="prev()">previous</div>
【问题讨论】:
标签: javascript angular ecmascript-6 angular-material array-indexing