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 yesterday









      Yufenyuy Veyeh Dider

      149111




      149111










      asked Nov 10 at 14:24









      Priyanka

      34




      34
























          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
            yesterday










          • Updated the answer with <img [src]= horizontal.items.items[selectedIndex].icon class="icon"/>
            – Sunil Singh
            yesterday










          • 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
            yesterday










          • I updated the answer in edit section. Please check if it works for you.
            – Sunil Singh
            yesterday










          • It is still not working for me. could you please give me any other solution if its there? please..
            – Priyanka
            yesterday











          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
































          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
            yesterday










          • Updated the answer with <img [src]= horizontal.items.items[selectedIndex].icon class="icon"/>
            – Sunil Singh
            yesterday










          • 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
            yesterday










          • I updated the answer in edit section. Please check if it works for you.
            – Sunil Singh
            yesterday










          • It is still not working for me. could you please give me any other solution if its there? please..
            – Priyanka
            yesterday















          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
            yesterday










          • Updated the answer with <img [src]= horizontal.items.items[selectedIndex].icon class="icon"/>
            – Sunil Singh
            yesterday










          • 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
            yesterday










          • I updated the answer in edit section. Please check if it works for you.
            – Sunil Singh
            yesterday










          • It is still not working for me. could you please give me any other solution if its there? please..
            – Priyanka
            yesterday













          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 yesterday

























          answered Nov 10 at 15:59









          Sunil Singh

          4,6811624




          4,6811624












          • 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
            yesterday










          • Updated the answer with <img [src]= horizontal.items.items[selectedIndex].icon class="icon"/>
            – Sunil Singh
            yesterday










          • 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
            yesterday










          • I updated the answer in edit section. Please check if it works for you.
            – Sunil Singh
            yesterday










          • It is still not working for me. could you please give me any other solution if its there? please..
            – Priyanka
            yesterday


















          • 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
            yesterday










          • Updated the answer with <img [src]= horizontal.items.items[selectedIndex].icon class="icon"/>
            – Sunil Singh
            yesterday










          • 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
            yesterday










          • I updated the answer in edit section. Please check if it works for you.
            – Sunil Singh
            yesterday










          • It is still not working for me. could you please give me any other solution if its there? please..
            – Priyanka
            yesterday
















          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
          yesterday




          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
          yesterday












          Updated the answer with <img [src]= horizontal.items.items[selectedIndex].icon class="icon"/>
          – Sunil Singh
          yesterday




          Updated the answer with <img [src]= horizontal.items.items[selectedIndex].icon class="icon"/>
          – Sunil Singh
          yesterday












          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
          yesterday




          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
          yesterday












          I updated the answer in edit section. Please check if it works for you.
          – Sunil Singh
          yesterday




          I updated the answer in edit section. Please check if it works for you.
          – Sunil Singh
          yesterday












          It is still not working for me. could you please give me any other solution if its there? please..
          – Priyanka
          yesterday




          It is still not working for me. could you please give me any other solution if its there? please..
          – Priyanka
          yesterday


















           

          draft saved


          draft discarded



















































           


          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




















































































          Popular posts from this blog

          List item for chat from Array inside array React Native

          Thiostrepton

          Caerphilly