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?
html css angular angular5 angular6
add a comment |
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?
html css angular angular5 angular6
add a comment |
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?
html css angular angular5 angular6
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
html css angular angular5 angular6
edited Nov 12 at 15:49
Yufenyuy Veyeh Dider
147111
147111
asked Nov 10 at 14:24
Priyanka
87
87
add a comment |
add a comment |
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;
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
add a comment |
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;
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
add a comment |
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;
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
add a comment |
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;
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;
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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