How to give condition in image tag using angular 6?
up vote
1
down vote
favorite
My webpage contains,
<ng-container *ngFor="let horizontal of categories">
<ng-container *ngFor="let horizontalval of horizontal.items;let i=index">
<button [ngClass]="'selected-color' : i==selectedIndex "
[ngStyle]="'padding-top':horizontal.items[i].title==='Business'?'2.56%':(horizontal.items[i].title==='Plat' ? '2.56%':null)"type="submit" class="btn1" [id]="horizontal.items[i].title"
(click)="changeImage(id);">
<img [src]= horizontal.items[i].icon alt="image not found" class="icon"/>
horizontal.items[i].title
</button>
</ng-container>
My ts file:
changeImage(option)
if(option == "Trans")
this.imgDefault = 'imgChange';
console.log("this.imgDefault :",this.imgDefault);
else if(option == "Col")
this.imgDefault = 'imgChange1';
console.log("this.imgDefault :",this.imgDefault1);
else if(option == "Business")
this.imgDefault = 'imgChange2';
console.log("this.imgDefault :",this.imgDefault2);
So in my css I have kept the images which needs to be changed onclick of the button.
.imgChange
background-image: url('../assets/Trans_2.png');
color:blue;
.imgChange1
background-image: url('../assets/Trans_3.png');
color:blue;
.imgChange2
background-image: url('../assets/Trans_4.png');
color:blue;
How can I give condition in img tag, so that onclick of a button to change the image?
html css angular angular6
add a comment |
up vote
1
down vote
favorite
My webpage contains,
<ng-container *ngFor="let horizontal of categories">
<ng-container *ngFor="let horizontalval of horizontal.items;let i=index">
<button [ngClass]="'selected-color' : i==selectedIndex "
[ngStyle]="'padding-top':horizontal.items[i].title==='Business'?'2.56%':(horizontal.items[i].title==='Plat' ? '2.56%':null)"type="submit" class="btn1" [id]="horizontal.items[i].title"
(click)="changeImage(id);">
<img [src]= horizontal.items[i].icon alt="image not found" class="icon"/>
horizontal.items[i].title
</button>
</ng-container>
My ts file:
changeImage(option)
if(option == "Trans")
this.imgDefault = 'imgChange';
console.log("this.imgDefault :",this.imgDefault);
else if(option == "Col")
this.imgDefault = 'imgChange1';
console.log("this.imgDefault :",this.imgDefault1);
else if(option == "Business")
this.imgDefault = 'imgChange2';
console.log("this.imgDefault :",this.imgDefault2);
So in my css I have kept the images which needs to be changed onclick of the button.
.imgChange
background-image: url('../assets/Trans_2.png');
color:blue;
.imgChange1
background-image: url('../assets/Trans_3.png');
color:blue;
.imgChange2
background-image: url('../assets/Trans_4.png');
color:blue;
How can I give condition in img tag, so that onclick of a button to change the image?
html css angular angular6
First of all, make your decision how you want to display the image. Either by img src or setting the background image by css ?
– Sunil Singh
Nov 11 at 18:26
@Sai, accept answer if you found one.
– Mahi
Nov 12 at 17:54
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
My webpage contains,
<ng-container *ngFor="let horizontal of categories">
<ng-container *ngFor="let horizontalval of horizontal.items;let i=index">
<button [ngClass]="'selected-color' : i==selectedIndex "
[ngStyle]="'padding-top':horizontal.items[i].title==='Business'?'2.56%':(horizontal.items[i].title==='Plat' ? '2.56%':null)"type="submit" class="btn1" [id]="horizontal.items[i].title"
(click)="changeImage(id);">
<img [src]= horizontal.items[i].icon alt="image not found" class="icon"/>
horizontal.items[i].title
</button>
</ng-container>
My ts file:
changeImage(option)
if(option == "Trans")
this.imgDefault = 'imgChange';
console.log("this.imgDefault :",this.imgDefault);
else if(option == "Col")
this.imgDefault = 'imgChange1';
console.log("this.imgDefault :",this.imgDefault1);
else if(option == "Business")
this.imgDefault = 'imgChange2';
console.log("this.imgDefault :",this.imgDefault2);
So in my css I have kept the images which needs to be changed onclick of the button.
.imgChange
background-image: url('../assets/Trans_2.png');
color:blue;
.imgChange1
background-image: url('../assets/Trans_3.png');
color:blue;
.imgChange2
background-image: url('../assets/Trans_4.png');
color:blue;
How can I give condition in img tag, so that onclick of a button to change the image?
html css angular angular6
My webpage contains,
<ng-container *ngFor="let horizontal of categories">
<ng-container *ngFor="let horizontalval of horizontal.items;let i=index">
<button [ngClass]="'selected-color' : i==selectedIndex "
[ngStyle]="'padding-top':horizontal.items[i].title==='Business'?'2.56%':(horizontal.items[i].title==='Plat' ? '2.56%':null)"type="submit" class="btn1" [id]="horizontal.items[i].title"
(click)="changeImage(id);">
<img [src]= horizontal.items[i].icon alt="image not found" class="icon"/>
horizontal.items[i].title
</button>
</ng-container>
My ts file:
changeImage(option)
if(option == "Trans")
this.imgDefault = 'imgChange';
console.log("this.imgDefault :",this.imgDefault);
else if(option == "Col")
this.imgDefault = 'imgChange1';
console.log("this.imgDefault :",this.imgDefault1);
else if(option == "Business")
this.imgDefault = 'imgChange2';
console.log("this.imgDefault :",this.imgDefault2);
So in my css I have kept the images which needs to be changed onclick of the button.
.imgChange
background-image: url('../assets/Trans_2.png');
color:blue;
.imgChange1
background-image: url('../assets/Trans_3.png');
color:blue;
.imgChange2
background-image: url('../assets/Trans_4.png');
color:blue;
How can I give condition in img tag, so that onclick of a button to change the image?
html css angular angular6
html css angular angular6
edited Nov 11 at 16:51
asked Nov 11 at 15:57
sai
163
163
First of all, make your decision how you want to display the image. Either by img src or setting the background image by css ?
– Sunil Singh
Nov 11 at 18:26
@Sai, accept answer if you found one.
– Mahi
Nov 12 at 17:54
add a comment |
First of all, make your decision how you want to display the image. Either by img src or setting the background image by css ?
– Sunil Singh
Nov 11 at 18:26
@Sai, accept answer if you found one.
– Mahi
Nov 12 at 17:54
First of all, make your decision how you want to display the image. Either by img src or setting the background image by css ?
– Sunil Singh
Nov 11 at 18:26
First of all, make your decision how you want to display the image. Either by img src or setting the background image by css ?
– Sunil Singh
Nov 11 at 18:26
@Sai, accept answer if you found one.
– Mahi
Nov 12 at 17:54
@Sai, accept answer if you found one.
– Mahi
Nov 12 at 17:54
add a comment |
2 Answers
2
active
oldest
votes
up vote
3
down vote
You should add image in a div as below:-
ts
imgDefault = 'imgChange'; <-- default;
changeImage(option)
if(option == "Trans")
this.imgDefault = 'imgChange';
console.log("this.imgDefault :",this.imgDefault);
else if(option == "Col")
this.imgDefault = 'imgChange1';
console.log("this.imgDefault :",this.imgDefault1);
else if(option == "Business Process Re-engineering")
this.imgDefault = 'imgChange2';
console.log("this.imgDefault :",this.imgDefault2);
.html
<ng-container *ngFor="let horizontal of categories">
<ng-container *ngFor="let horizontalval of horizontal.items;let i=index">
<button [ngClass]="'selected-color' : i==selectedIndex "
[ngStyle]="'padding-top':horizontal.items[i].title==='Business'?'2.56%':(horizontal.items[i].title==='Plat' ? '2.56%':null)"type="submit" class="btn1" [id]="horizontal.items[i].title"click)="changeImage(id);">
horizontal.items[i].title
</button>
</ng-container>
</ng-container>
<div [ngClass]="imgDefault"></div>
Link:- https://stackblitz.com/edit/angular6-sqemmd?file=app/app.component.html
Instead of <ng-container>. You should use <div>
– Mahi
Nov 11 at 16:13
Thanks for your reply. I tried the same. But Im not able to get the image.
– sai
Nov 11 at 16:46
Is this.imgDefault is getting changed onclick?
– Mahi
Nov 11 at 16:51
Yes, It needs to be changed on onclick of the button
– sai
Nov 11 at 17:01
Inspect the div and check whether image is getting loaded on network tab
– Mahi
Nov 11 at 17:03
|
show 4 more comments
up vote
0
down vote
You can directly add class names based on the conditions - you are trying to add images from css replace the img tag as div which will render background image based on the class applied
Make sure if you want your images in the loop, if not move your div outside *ngFor
<div [class.imgChange]="imgDefault === 'imgChange'"
[class.imgChange1]="imgDefault === 'imgChange1'"
[class.imgChange2]="imgDefault === 'imgChange2'" ></div>
This will add all your classes based on the condition and the div will render the img - Hope this works Happy coding !!
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
You should add image in a div as below:-
ts
imgDefault = 'imgChange'; <-- default;
changeImage(option)
if(option == "Trans")
this.imgDefault = 'imgChange';
console.log("this.imgDefault :",this.imgDefault);
else if(option == "Col")
this.imgDefault = 'imgChange1';
console.log("this.imgDefault :",this.imgDefault1);
else if(option == "Business Process Re-engineering")
this.imgDefault = 'imgChange2';
console.log("this.imgDefault :",this.imgDefault2);
.html
<ng-container *ngFor="let horizontal of categories">
<ng-container *ngFor="let horizontalval of horizontal.items;let i=index">
<button [ngClass]="'selected-color' : i==selectedIndex "
[ngStyle]="'padding-top':horizontal.items[i].title==='Business'?'2.56%':(horizontal.items[i].title==='Plat' ? '2.56%':null)"type="submit" class="btn1" [id]="horizontal.items[i].title"click)="changeImage(id);">
horizontal.items[i].title
</button>
</ng-container>
</ng-container>
<div [ngClass]="imgDefault"></div>
Link:- https://stackblitz.com/edit/angular6-sqemmd?file=app/app.component.html
Instead of <ng-container>. You should use <div>
– Mahi
Nov 11 at 16:13
Thanks for your reply. I tried the same. But Im not able to get the image.
– sai
Nov 11 at 16:46
Is this.imgDefault is getting changed onclick?
– Mahi
Nov 11 at 16:51
Yes, It needs to be changed on onclick of the button
– sai
Nov 11 at 17:01
Inspect the div and check whether image is getting loaded on network tab
– Mahi
Nov 11 at 17:03
|
show 4 more comments
up vote
3
down vote
You should add image in a div as below:-
ts
imgDefault = 'imgChange'; <-- default;
changeImage(option)
if(option == "Trans")
this.imgDefault = 'imgChange';
console.log("this.imgDefault :",this.imgDefault);
else if(option == "Col")
this.imgDefault = 'imgChange1';
console.log("this.imgDefault :",this.imgDefault1);
else if(option == "Business Process Re-engineering")
this.imgDefault = 'imgChange2';
console.log("this.imgDefault :",this.imgDefault2);
.html
<ng-container *ngFor="let horizontal of categories">
<ng-container *ngFor="let horizontalval of horizontal.items;let i=index">
<button [ngClass]="'selected-color' : i==selectedIndex "
[ngStyle]="'padding-top':horizontal.items[i].title==='Business'?'2.56%':(horizontal.items[i].title==='Plat' ? '2.56%':null)"type="submit" class="btn1" [id]="horizontal.items[i].title"click)="changeImage(id);">
horizontal.items[i].title
</button>
</ng-container>
</ng-container>
<div [ngClass]="imgDefault"></div>
Link:- https://stackblitz.com/edit/angular6-sqemmd?file=app/app.component.html
Instead of <ng-container>. You should use <div>
– Mahi
Nov 11 at 16:13
Thanks for your reply. I tried the same. But Im not able to get the image.
– sai
Nov 11 at 16:46
Is this.imgDefault is getting changed onclick?
– Mahi
Nov 11 at 16:51
Yes, It needs to be changed on onclick of the button
– sai
Nov 11 at 17:01
Inspect the div and check whether image is getting loaded on network tab
– Mahi
Nov 11 at 17:03
|
show 4 more comments
up vote
3
down vote
up vote
3
down vote
You should add image in a div as below:-
ts
imgDefault = 'imgChange'; <-- default;
changeImage(option)
if(option == "Trans")
this.imgDefault = 'imgChange';
console.log("this.imgDefault :",this.imgDefault);
else if(option == "Col")
this.imgDefault = 'imgChange1';
console.log("this.imgDefault :",this.imgDefault1);
else if(option == "Business Process Re-engineering")
this.imgDefault = 'imgChange2';
console.log("this.imgDefault :",this.imgDefault2);
.html
<ng-container *ngFor="let horizontal of categories">
<ng-container *ngFor="let horizontalval of horizontal.items;let i=index">
<button [ngClass]="'selected-color' : i==selectedIndex "
[ngStyle]="'padding-top':horizontal.items[i].title==='Business'?'2.56%':(horizontal.items[i].title==='Plat' ? '2.56%':null)"type="submit" class="btn1" [id]="horizontal.items[i].title"click)="changeImage(id);">
horizontal.items[i].title
</button>
</ng-container>
</ng-container>
<div [ngClass]="imgDefault"></div>
Link:- https://stackblitz.com/edit/angular6-sqemmd?file=app/app.component.html
You should add image in a div as below:-
ts
imgDefault = 'imgChange'; <-- default;
changeImage(option)
if(option == "Trans")
this.imgDefault = 'imgChange';
console.log("this.imgDefault :",this.imgDefault);
else if(option == "Col")
this.imgDefault = 'imgChange1';
console.log("this.imgDefault :",this.imgDefault1);
else if(option == "Business Process Re-engineering")
this.imgDefault = 'imgChange2';
console.log("this.imgDefault :",this.imgDefault2);
.html
<ng-container *ngFor="let horizontal of categories">
<ng-container *ngFor="let horizontalval of horizontal.items;let i=index">
<button [ngClass]="'selected-color' : i==selectedIndex "
[ngStyle]="'padding-top':horizontal.items[i].title==='Business'?'2.56%':(horizontal.items[i].title==='Plat' ? '2.56%':null)"type="submit" class="btn1" [id]="horizontal.items[i].title"click)="changeImage(id);">
horizontal.items[i].title
</button>
</ng-container>
</ng-container>
<div [ngClass]="imgDefault"></div>
Link:- https://stackblitz.com/edit/angular6-sqemmd?file=app/app.component.html
edited Nov 13 at 5:57
answered Nov 11 at 16:10
Mahi
824319
824319
Instead of <ng-container>. You should use <div>
– Mahi
Nov 11 at 16:13
Thanks for your reply. I tried the same. But Im not able to get the image.
– sai
Nov 11 at 16:46
Is this.imgDefault is getting changed onclick?
– Mahi
Nov 11 at 16:51
Yes, It needs to be changed on onclick of the button
– sai
Nov 11 at 17:01
Inspect the div and check whether image is getting loaded on network tab
– Mahi
Nov 11 at 17:03
|
show 4 more comments
Instead of <ng-container>. You should use <div>
– Mahi
Nov 11 at 16:13
Thanks for your reply. I tried the same. But Im not able to get the image.
– sai
Nov 11 at 16:46
Is this.imgDefault is getting changed onclick?
– Mahi
Nov 11 at 16:51
Yes, It needs to be changed on onclick of the button
– sai
Nov 11 at 17:01
Inspect the div and check whether image is getting loaded on network tab
– Mahi
Nov 11 at 17:03
Instead of <ng-container>. You should use <div>
– Mahi
Nov 11 at 16:13
Instead of <ng-container>. You should use <div>
– Mahi
Nov 11 at 16:13
Thanks for your reply. I tried the same. But Im not able to get the image.
– sai
Nov 11 at 16:46
Thanks for your reply. I tried the same. But Im not able to get the image.
– sai
Nov 11 at 16:46
Is this.imgDefault is getting changed onclick?
– Mahi
Nov 11 at 16:51
Is this.imgDefault is getting changed onclick?
– Mahi
Nov 11 at 16:51
Yes, It needs to be changed on onclick of the button
– sai
Nov 11 at 17:01
Yes, It needs to be changed on onclick of the button
– sai
Nov 11 at 17:01
Inspect the div and check whether image is getting loaded on network tab
– Mahi
Nov 11 at 17:03
Inspect the div and check whether image is getting loaded on network tab
– Mahi
Nov 11 at 17:03
|
show 4 more comments
up vote
0
down vote
You can directly add class names based on the conditions - you are trying to add images from css replace the img tag as div which will render background image based on the class applied
Make sure if you want your images in the loop, if not move your div outside *ngFor
<div [class.imgChange]="imgDefault === 'imgChange'"
[class.imgChange1]="imgDefault === 'imgChange1'"
[class.imgChange2]="imgDefault === 'imgChange2'" ></div>
This will add all your classes based on the condition and the div will render the img - Hope this works Happy coding !!
add a comment |
up vote
0
down vote
You can directly add class names based on the conditions - you are trying to add images from css replace the img tag as div which will render background image based on the class applied
Make sure if you want your images in the loop, if not move your div outside *ngFor
<div [class.imgChange]="imgDefault === 'imgChange'"
[class.imgChange1]="imgDefault === 'imgChange1'"
[class.imgChange2]="imgDefault === 'imgChange2'" ></div>
This will add all your classes based on the condition and the div will render the img - Hope this works Happy coding !!
add a comment |
up vote
0
down vote
up vote
0
down vote
You can directly add class names based on the conditions - you are trying to add images from css replace the img tag as div which will render background image based on the class applied
Make sure if you want your images in the loop, if not move your div outside *ngFor
<div [class.imgChange]="imgDefault === 'imgChange'"
[class.imgChange1]="imgDefault === 'imgChange1'"
[class.imgChange2]="imgDefault === 'imgChange2'" ></div>
This will add all your classes based on the condition and the div will render the img - Hope this works Happy coding !!
You can directly add class names based on the conditions - you are trying to add images from css replace the img tag as div which will render background image based on the class applied
Make sure if you want your images in the loop, if not move your div outside *ngFor
<div [class.imgChange]="imgDefault === 'imgChange'"
[class.imgChange1]="imgDefault === 'imgChange1'"
[class.imgChange2]="imgDefault === 'imgChange2'" ></div>
This will add all your classes based on the condition and the div will render the img - Hope this works Happy coding !!
edited Nov 11 at 18:42
answered Nov 11 at 18:37
Rahul
9381315
9381315
add a comment |
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%2f53250498%2fhow-to-give-condition-in-image-tag-using-angular-6%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
First of all, make your decision how you want to display the image. Either by img src or setting the background image by css ?
– Sunil Singh
Nov 11 at 18:26
@Sai, accept answer if you found one.
– Mahi
Nov 12 at 17:54