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 yesterday
Yufenyuy Veyeh Dider
149111
149111
asked Nov 10 at 14:24
Priyanka
34
34
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
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
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
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
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
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
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 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
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
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
add a comment |
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
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
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
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
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