I'm trying to create simple image modal using angular material. next()
supposed to be view next image from the array and prev()
view previous image.
Problem next and prev function not working as expected. If click next() index only increase +1 then stopped. If I click prev() index become -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>