How to change the images dynamically inside for loop in angular6?









up vote
0
down vote

favorite












My html code:



 <div class="row">
<div class="col-xs-6 col-sm-6 col-md-4 col-lg-12">
<ng-container *ngFor="let horizontal of categories">
<ng-container *ngFor="let horizontalval of horizontal.items;let i=index">
<button [ngClass]="'selected-color' : i==selectedIndex" type="submit" class="btn1" [id]="horizontal.items[i].title" (click)="changeTrans(horizontal.items[i].title);changeColor(i)">
<img [src]= horizontal.items[i].icon alt="image not found" class="icon"/>
horizontal.items[i].title
</button>
</ng-container>
</ng-container></div>
</div>


So my component.ts file,



 changeColor(i)
console.log(i);
this.selectedIndex = i;



my css:



.selected-color
background-color: orange;
color:white;
// background-image: url('./../../../../../assets/Trans_1.png');



So here I am able to change the color of the selected button, but I also need to change different images for different values. Here, how can I change my image dynamically for every button id?



Basically what I want to know is, how to change the image dynamically for every button which is going to be displayed via for. Can anybody suggest me something to achieve it?










share|improve this question



























    up vote
    0
    down vote

    favorite












    My html code:



     <div class="row">
    <div class="col-xs-6 col-sm-6 col-md-4 col-lg-12">
    <ng-container *ngFor="let horizontal of categories">
    <ng-container *ngFor="let horizontalval of horizontal.items;let i=index">
    <button [ngClass]="'selected-color' : i==selectedIndex" type="submit" class="btn1" [id]="horizontal.items[i].title" (click)="changeTrans(horizontal.items[i].title);changeColor(i)">
    <img [src]= horizontal.items[i].icon alt="image not found" class="icon"/>
    horizontal.items[i].title
    </button>
    </ng-container>
    </ng-container></div>
    </div>


    So my component.ts file,



     changeColor(i)
    console.log(i);
    this.selectedIndex = i;



    my css:



    .selected-color
    background-color: orange;
    color:white;
    // background-image: url('./../../../../../assets/Trans_1.png');



    So here I am able to change the color of the selected button, but I also need to change different images for different values. Here, how can I change my image dynamically for every button id?



    Basically what I want to know is, how to change the image dynamically for every button which is going to be displayed via for. Can anybody suggest me something to achieve it?










    share|improve this question

























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      My html code:



       <div class="row">
      <div class="col-xs-6 col-sm-6 col-md-4 col-lg-12">
      <ng-container *ngFor="let horizontal of categories">
      <ng-container *ngFor="let horizontalval of horizontal.items;let i=index">
      <button [ngClass]="'selected-color' : i==selectedIndex" type="submit" class="btn1" [id]="horizontal.items[i].title" (click)="changeTrans(horizontal.items[i].title);changeColor(i)">
      <img [src]= horizontal.items[i].icon alt="image not found" class="icon"/>
      horizontal.items[i].title
      </button>
      </ng-container>
      </ng-container></div>
      </div>


      So my component.ts file,



       changeColor(i)
      console.log(i);
      this.selectedIndex = i;



      my css:



      .selected-color
      background-color: orange;
      color:white;
      // background-image: url('./../../../../../assets/Trans_1.png');



      So here I am able to change the color of the selected button, but I also need to change different images for different values. Here, how can I change my image dynamically for every button id?



      Basically what I want to know is, how to change the image dynamically for every button which is going to be displayed via for. Can anybody suggest me something to achieve it?










      share|improve this question















      My html code:



       <div class="row">
      <div class="col-xs-6 col-sm-6 col-md-4 col-lg-12">
      <ng-container *ngFor="let horizontal of categories">
      <ng-container *ngFor="let horizontalval of horizontal.items;let i=index">
      <button [ngClass]="'selected-color' : i==selectedIndex" type="submit" class="btn1" [id]="horizontal.items[i].title" (click)="changeTrans(horizontal.items[i].title);changeColor(i)">
      <img [src]= horizontal.items[i].icon alt="image not found" class="icon"/>
      horizontal.items[i].title
      </button>
      </ng-container>
      </ng-container></div>
      </div>


      So my component.ts file,



       changeColor(i)
      console.log(i);
      this.selectedIndex = i;



      my css:



      .selected-color
      background-color: orange;
      color:white;
      // background-image: url('./../../../../../assets/Trans_1.png');



      So here I am able to change the color of the selected button, but I also need to change different images for different values. Here, how can I change my image dynamically for every button id?



      Basically what I want to know is, how to change the image dynamically for every button which is going to be displayed via for. Can anybody suggest me something to achieve it?







      html css angular angular5 angular6






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 12 at 15:49









      Yufenyuy Veyeh Dider

      147111




      147111










      asked Nov 10 at 14:24









      Priyanka

      87




      87






















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          1
          down vote













          Replace the below code



          <ng-container *ngFor="let horizontalval of horizontal.items;let i=index">


          by



          <img [attr.src]= "horizontalval.icon" alt="image not found" class="icon"/>


          Edit



          Note : object horizontalval should have attribute icon which will have path of image.



          If you want to change the path for entire image then you can use selectedIndex.



          <img [src]= "horizontal.items[selectedIndex].icon" class="icon"/>


          Edit



           <button [ngClass]="'selected-color' : horizontal.items[i].selected" 
          type="submit" class="btn1" [id]="horizontal.items[i].title"
          (click)="changeTrans(horizontal.items[i].title);
          changeColor(horizontal.items[i])">
          <img [src]= horizontal.items[i].icon alt="image not found" class="icon"/>
          horizontal.items[i].title
          </button>


          ts



          changeColor(item)
          this.item.selected = true;






          share|improve this answer






















          • Thanks for the reply. I can see the images of the button onload. What I want is onclick of the button I need to replace the image with another image for every buttons. Could you please tell me how to do that?
            – Priyanka
            Nov 12 at 6:37










          • Updated the answer with <img [src]= horizontal.items.items[selectedIndex].icon class="icon"/>
            – Sunil Singh
            Nov 12 at 6:51










          • Thank you so much. Image is changing, but for every button Im getting the same images. (ex. If I click the first button, the image of the button needs to be changed to aaa, if i click the 2nd button the image of the button needs to be changed to bbb)
            – Priyanka
            Nov 12 at 13:40










          • I updated the answer in edit section. Please check if it works for you.
            – Sunil Singh
            Nov 12 at 15:11










          • It is still not working for me. could you please give me any other solution if its there? please..
            – Priyanka
            Nov 12 at 15:24










          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',
          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%2f53239898%2fhow-to-change-the-images-dynamically-inside-for-loop-in-angular6%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








          up vote
          1
          down vote













          Replace the below code



          <ng-container *ngFor="let horizontalval of horizontal.items;let i=index">


          by



          <img [attr.src]= "horizontalval.icon" alt="image not found" class="icon"/>


          Edit



          Note : object horizontalval should have attribute icon which will have path of image.



          If you want to change the path for entire image then you can use selectedIndex.



          <img [src]= "horizontal.items[selectedIndex].icon" class="icon"/>


          Edit



           <button [ngClass]="'selected-color' : horizontal.items[i].selected" 
          type="submit" class="btn1" [id]="horizontal.items[i].title"
          (click)="changeTrans(horizontal.items[i].title);
          changeColor(horizontal.items[i])">
          <img [src]= horizontal.items[i].icon alt="image not found" class="icon"/>
          horizontal.items[i].title
          </button>


          ts



          changeColor(item)
          this.item.selected = true;






          share|improve this answer






















          • Thanks for the reply. I can see the images of the button onload. What I want is onclick of the button I need to replace the image with another image for every buttons. Could you please tell me how to do that?
            – Priyanka
            Nov 12 at 6:37










          • Updated the answer with <img [src]= horizontal.items.items[selectedIndex].icon class="icon"/>
            – Sunil Singh
            Nov 12 at 6:51










          • Thank you so much. Image is changing, but for every button Im getting the same images. (ex. If I click the first button, the image of the button needs to be changed to aaa, if i click the 2nd button the image of the button needs to be changed to bbb)
            – Priyanka
            Nov 12 at 13:40










          • I updated the answer in edit section. Please check if it works for you.
            – Sunil Singh
            Nov 12 at 15:11










          • It is still not working for me. could you please give me any other solution if its there? please..
            – Priyanka
            Nov 12 at 15:24














          up vote
          1
          down vote













          Replace the below code



          <ng-container *ngFor="let horizontalval of horizontal.items;let i=index">


          by



          <img [attr.src]= "horizontalval.icon" alt="image not found" class="icon"/>


          Edit



          Note : object horizontalval should have attribute icon which will have path of image.



          If you want to change the path for entire image then you can use selectedIndex.



          <img [src]= "horizontal.items[selectedIndex].icon" class="icon"/>


          Edit



           <button [ngClass]="'selected-color' : horizontal.items[i].selected" 
          type="submit" class="btn1" [id]="horizontal.items[i].title"
          (click)="changeTrans(horizontal.items[i].title);
          changeColor(horizontal.items[i])">
          <img [src]= horizontal.items[i].icon alt="image not found" class="icon"/>
          horizontal.items[i].title
          </button>


          ts



          changeColor(item)
          this.item.selected = true;






          share|improve this answer






















          • Thanks for the reply. I can see the images of the button onload. What I want is onclick of the button I need to replace the image with another image for every buttons. Could you please tell me how to do that?
            – Priyanka
            Nov 12 at 6:37










          • Updated the answer with <img [src]= horizontal.items.items[selectedIndex].icon class="icon"/>
            – Sunil Singh
            Nov 12 at 6:51










          • Thank you so much. Image is changing, but for every button Im getting the same images. (ex. If I click the first button, the image of the button needs to be changed to aaa, if i click the 2nd button the image of the button needs to be changed to bbb)
            – Priyanka
            Nov 12 at 13:40










          • I updated the answer in edit section. Please check if it works for you.
            – Sunil Singh
            Nov 12 at 15:11










          • It is still not working for me. could you please give me any other solution if its there? please..
            – Priyanka
            Nov 12 at 15:24












          up vote
          1
          down vote










          up vote
          1
          down vote









          Replace the below code



          <ng-container *ngFor="let horizontalval of horizontal.items;let i=index">


          by



          <img [attr.src]= "horizontalval.icon" alt="image not found" class="icon"/>


          Edit



          Note : object horizontalval should have attribute icon which will have path of image.



          If you want to change the path for entire image then you can use selectedIndex.



          <img [src]= "horizontal.items[selectedIndex].icon" class="icon"/>


          Edit



           <button [ngClass]="'selected-color' : horizontal.items[i].selected" 
          type="submit" class="btn1" [id]="horizontal.items[i].title"
          (click)="changeTrans(horizontal.items[i].title);
          changeColor(horizontal.items[i])">
          <img [src]= horizontal.items[i].icon alt="image not found" class="icon"/>
          horizontal.items[i].title
          </button>


          ts



          changeColor(item)
          this.item.selected = true;






          share|improve this answer














          Replace the below code



          <ng-container *ngFor="let horizontalval of horizontal.items;let i=index">


          by



          <img [attr.src]= "horizontalval.icon" alt="image not found" class="icon"/>


          Edit



          Note : object horizontalval should have attribute icon which will have path of image.



          If you want to change the path for entire image then you can use selectedIndex.



          <img [src]= "horizontal.items[selectedIndex].icon" class="icon"/>


          Edit



           <button [ngClass]="'selected-color' : horizontal.items[i].selected" 
          type="submit" class="btn1" [id]="horizontal.items[i].title"
          (click)="changeTrans(horizontal.items[i].title);
          changeColor(horizontal.items[i])">
          <img [src]= horizontal.items[i].icon alt="image not found" class="icon"/>
          horizontal.items[i].title
          </button>


          ts



          changeColor(item)
          this.item.selected = true;







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 12 at 15:10

























          answered Nov 10 at 15:59









          Sunil Singh

          6,0711626




          6,0711626











          • Thanks for the reply. I can see the images of the button onload. What I want is onclick of the button I need to replace the image with another image for every buttons. Could you please tell me how to do that?
            – Priyanka
            Nov 12 at 6:37










          • Updated the answer with <img [src]= horizontal.items.items[selectedIndex].icon class="icon"/>
            – Sunil Singh
            Nov 12 at 6:51










          • Thank you so much. Image is changing, but for every button Im getting the same images. (ex. If I click the first button, the image of the button needs to be changed to aaa, if i click the 2nd button the image of the button needs to be changed to bbb)
            – Priyanka
            Nov 12 at 13:40










          • I updated the answer in edit section. Please check if it works for you.
            – Sunil Singh
            Nov 12 at 15:11










          • It is still not working for me. could you please give me any other solution if its there? please..
            – Priyanka
            Nov 12 at 15:24
















          • Thanks for the reply. I can see the images of the button onload. What I want is onclick of the button I need to replace the image with another image for every buttons. Could you please tell me how to do that?
            – Priyanka
            Nov 12 at 6:37










          • Updated the answer with <img [src]= horizontal.items.items[selectedIndex].icon class="icon"/>
            – Sunil Singh
            Nov 12 at 6:51










          • Thank you so much. Image is changing, but for every button Im getting the same images. (ex. If I click the first button, the image of the button needs to be changed to aaa, if i click the 2nd button the image of the button needs to be changed to bbb)
            – Priyanka
            Nov 12 at 13:40










          • I updated the answer in edit section. Please check if it works for you.
            – Sunil Singh
            Nov 12 at 15:11










          • It is still not working for me. could you please give me any other solution if its there? please..
            – Priyanka
            Nov 12 at 15:24















          Thanks for the reply. I can see the images of the button onload. What I want is onclick of the button I need to replace the image with another image for every buttons. Could you please tell me how to do that?
          – Priyanka
          Nov 12 at 6:37




          Thanks for the reply. I can see the images of the button onload. What I want is onclick of the button I need to replace the image with another image for every buttons. Could you please tell me how to do that?
          – Priyanka
          Nov 12 at 6:37












          Updated the answer with <img [src]= horizontal.items.items[selectedIndex].icon class="icon"/>
          – Sunil Singh
          Nov 12 at 6:51




          Updated the answer with <img [src]= horizontal.items.items[selectedIndex].icon class="icon"/>
          – Sunil Singh
          Nov 12 at 6:51












          Thank you so much. Image is changing, but for every button Im getting the same images. (ex. If I click the first button, the image of the button needs to be changed to aaa, if i click the 2nd button the image of the button needs to be changed to bbb)
          – Priyanka
          Nov 12 at 13:40




          Thank you so much. Image is changing, but for every button Im getting the same images. (ex. If I click the first button, the image of the button needs to be changed to aaa, if i click the 2nd button the image of the button needs to be changed to bbb)
          – Priyanka
          Nov 12 at 13:40












          I updated the answer in edit section. Please check if it works for you.
          – Sunil Singh
          Nov 12 at 15:11




          I updated the answer in edit section. Please check if it works for you.
          – Sunil Singh
          Nov 12 at 15:11












          It is still not working for me. could you please give me any other solution if its there? please..
          – Priyanka
          Nov 12 at 15:24




          It is still not working for me. could you please give me any other solution if its there? please..
          – Priyanka
          Nov 12 at 15:24

















          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%2f53239898%2fhow-to-change-the-images-dynamically-inside-for-loop-in-angular6%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

          Use pre created SQLite database for Android project in kotlin

          Darth Vader #20

          Ondo