我正在尝试使用角度材料创建简单的图像模式。next()
应该从数组中查看下一个图像并prev()
查看上一个图像。
问题 next 和 prev 功能无法按预期工作。如果单击 next() 索引只增加 +1 然后停止。如果我单击 prev() 索引变为-1
应用程序.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];
}
应用程序.html
<img [src]="currentImage"
alt="customer document" />
<div (click)="next()">next</div>
<div (click)="prev()">previous</div>