0

I am loading 3 sets of dynamic images on a button click:

Before button click a default image has to be showed --> it is working as single image

On button click whatever images are there in the array based on the button id it has to load and display

Issues :

After clicking on data set one (i.e id : 1), based images have to load and overwrite the default image, but along with data set one image, the default image is also showing and 1st position image is missing.

Likewise whenever I click on the other button, the images are loading but it is displaying the previous image (last button last image) and also the 1st array index image is not displaying

The below is the default image before the button is clicked:

images =[

 {
"img": "https://dummyimage.com/200x200/000/fff.jpg&text=a"
 }
 ];

Buttons are generated using dynamic data:

 buttonData = [
       {
         "id": 1,
         "name":"Data Set One"
       },
       {
        "id": 2,
        "name":"Data Set Two"
      },
      {
        "id": 3,
        "name":"Data Set Three"
      }
     ]


     count: number;
     currentImgUrl: string ;

  constructor(){
        this.count = 0;
        this.setImgUrl();
  }




    next(): void {
      this.count++;
      if (this.count >= this.images.length)
        this.count = 0;
      this.setImgUrl();
    }

    prev(): void {
      this.count--;
      if (this.count < 0)
        this.count = this.images.length - 1;
      this.setImgUrl();
    }

    setImgUrl(): void {
      this.currentImgUrl = this.images[this.count].img;
    }

       getImageData(id){
      this.images=[];
     if(id===1){

       this.count =0;
       this.images = [
          {
            "img":"https://dummyimage.com/200x200/000/fff.jpg&text=B"
          },
          {
           "img":"https://dummyimage.com/200x200/000/fff.jpg&text=C"
         },
         {
           "img":"https://dummyimage.com/200x200/000/fff.jpg&text=D"
         },
         {
           "img":"https://dummyimage.com/200x200/000/fff.jpg&text=E"
         },
         {
           "img":"https://dummyimage.com/200x200/000/fff.jpg&text=F"
         },
         {
           "img":"https://dummyimage.com/200x200/000/fff.jpg&text=G"
         }
        ];
     }
     if(id===2){

      this.count =0;
      this.images = [
         {
           "img":"https://dummyimage.com/200x200/000/fff.jpg&text=H"
         },
         {
          "img":"https://dummyimage.com/200x200/000/fff.jpg&text=I"
        },
        {
          "img":"https://dummyimage.com/200x200/000/fff.jpg&text=J"
        }
       ];
    }
    if(id===3){

      this.count =0;
      this.images = [
          {
           "img":"https://dummyimage.com/200x200/000/fff.jpg&text=K"
         },
         {
          "img":"https://dummyimage.com/200x200/000/fff.jpg&text=L"
        },
        {
          "img":"https://dummyimage.com/200x200/000/fff.jpg&text=M"
        }
       ];
    }

    }

.html code

<div class="container">
  <div class="row">
     <div class="col-xs-3 help_modal">
                                    <div *ngFor="let btnData of buttonData">
                                        <button type="button" class="btn btn-primary" id="{{btnData.id}}" (click)="getImageData(btnData.id)">{{btnData.name}}</button>
                                    </div>
                                </div>

<div  class="col-xs-9">
                                    <img [src]="currentImgUrl" style="width: 50%">
                                </div>

  </div>


                                 <button type="button" (click)="prev()" [disabled]="count == 0" class="btn btn-primary">Prev</button>
                            <button type="button" (click)="next()" [disabled]="count == images.length - 1" class="btn btn-primary">Next</button>


</div> 

Here is my stackblitz working link: https://stackblitz.com/edit/angular-yoey9z

1 Answer 1

1

At line 116 call this.setImgUrl(); to reset your list.

Sign up to request clarification or add additional context in comments.

4 Comments

could u modifiy it in stack blitz
No. Dexter - I gave you the line number and the 17 characters to type on that line. I bet you can pull that off.
i inserted that in the line 116 still i m not gettin the result n 2nd index missing for every thing
I click the first button then hit next repeatedly. It displays B,C,D,E,F,G, click the second button and get H, I, J, hit the third button and get K,L,M. Those are exactly the images you've specified in the order you've specified them with none of them "missing". Explain?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.