Unable to load dynamic images on button click using Angular 6












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










share|improve this question





























    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










    share|improve this question



























      0












      0








      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










      share|improve this question















      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







      javascript angular typescript






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 13 '18 at 1:54

























      asked Nov 13 '18 at 1:33









      Dexter

      608




      608
























          1 Answer
          1






          active

          oldest

          votes


















          1














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






          share|improve this answer





















          • could u modifiy it in stack blitz
            – Dexter
            Nov 13 '18 at 1:50










          • No. Dexter - I gave you the line number and the 17 characters to type on that line. I bet you can pull that off.
            – Randy Casburn
            Nov 13 '18 at 1:51










          • i inserted that in the line 116 still i m not gettin the result n 2nd index missing for every thing
            – Dexter
            Nov 13 '18 at 1:52












          • 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?
            – Randy Casburn
            Nov 13 '18 at 1:55











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53272495%2funable-to-load-dynamic-images-on-button-click-using-angular-6%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














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






          share|improve this answer





















          • could u modifiy it in stack blitz
            – Dexter
            Nov 13 '18 at 1:50










          • No. Dexter - I gave you the line number and the 17 characters to type on that line. I bet you can pull that off.
            – Randy Casburn
            Nov 13 '18 at 1:51










          • i inserted that in the line 116 still i m not gettin the result n 2nd index missing for every thing
            – Dexter
            Nov 13 '18 at 1:52












          • 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?
            – Randy Casburn
            Nov 13 '18 at 1:55
















          1














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






          share|improve this answer





















          • could u modifiy it in stack blitz
            – Dexter
            Nov 13 '18 at 1:50










          • No. Dexter - I gave you the line number and the 17 characters to type on that line. I bet you can pull that off.
            – Randy Casburn
            Nov 13 '18 at 1:51










          • i inserted that in the line 116 still i m not gettin the result n 2nd index missing for every thing
            – Dexter
            Nov 13 '18 at 1:52












          • 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?
            – Randy Casburn
            Nov 13 '18 at 1:55














          1












          1








          1






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






          share|improve this answer












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







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 13 '18 at 1:47









          Randy Casburn

          4,2741318




          4,2741318












          • could u modifiy it in stack blitz
            – Dexter
            Nov 13 '18 at 1:50










          • No. Dexter - I gave you the line number and the 17 characters to type on that line. I bet you can pull that off.
            – Randy Casburn
            Nov 13 '18 at 1:51










          • i inserted that in the line 116 still i m not gettin the result n 2nd index missing for every thing
            – Dexter
            Nov 13 '18 at 1:52












          • 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?
            – Randy Casburn
            Nov 13 '18 at 1:55


















          • could u modifiy it in stack blitz
            – Dexter
            Nov 13 '18 at 1:50










          • No. Dexter - I gave you the line number and the 17 characters to type on that line. I bet you can pull that off.
            – Randy Casburn
            Nov 13 '18 at 1:51










          • i inserted that in the line 116 still i m not gettin the result n 2nd index missing for every thing
            – Dexter
            Nov 13 '18 at 1:52












          • 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?
            – Randy Casburn
            Nov 13 '18 at 1:55
















          could u modifiy it in stack blitz
          – Dexter
          Nov 13 '18 at 1:50




          could u modifiy it in stack blitz
          – Dexter
          Nov 13 '18 at 1:50












          No. Dexter - I gave you the line number and the 17 characters to type on that line. I bet you can pull that off.
          – Randy Casburn
          Nov 13 '18 at 1:51




          No. Dexter - I gave you the line number and the 17 characters to type on that line. I bet you can pull that off.
          – Randy Casburn
          Nov 13 '18 at 1:51












          i inserted that in the line 116 still i m not gettin the result n 2nd index missing for every thing
          – Dexter
          Nov 13 '18 at 1:52






          i inserted that in the line 116 still i m not gettin the result n 2nd index missing for every thing
          – Dexter
          Nov 13 '18 at 1:52














          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?
          – Randy Casburn
          Nov 13 '18 at 1:55




          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?
          – Randy Casburn
          Nov 13 '18 at 1:55


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53272495%2funable-to-load-dynamic-images-on-button-click-using-angular-6%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Bressuire

          Vorschmack

          Quarantine