How to give condition in image tag using angular 6?
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 |
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 '18 at 18:26
@Sai, accept answer if you found one.
– Mahi
Nov 12 '18 at 17:54
add a comment |
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 '18 at 16:51
sai
asked Nov 11 '18 at 15:57
saisai
1613
1613
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 '18 at 18:26
@Sai, accept answer if you found one.
– Mahi
Nov 12 '18 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 '18 at 18:26
@Sai, accept answer if you found one.
– Mahi
Nov 12 '18 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 '18 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 '18 at 18:26
@Sai, accept answer if you found one.
– Mahi
Nov 12 '18 at 17:54
@Sai, accept answer if you found one.
– Mahi
Nov 12 '18 at 17:54
add a comment |
2 Answers
2
active
oldest
votes
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 '18 at 16:13
Thanks for your reply. I tried the same. But Im not able to get the image.
– sai
Nov 11 '18 at 16:46
Is this.imgDefault is getting changed onclick?
– Mahi
Nov 11 '18 at 16:51
Yes, It needs to be changed on onclick of the button
– sai
Nov 11 '18 at 17:01
Inspect the div and check whether image is getting loaded on network tab
– Mahi
Nov 11 '18 at 17:03
|
show 4 more comments
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 |
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',
autoActivateHeartbeat: false,
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
);
);
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
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
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 '18 at 16:13
Thanks for your reply. I tried the same. But Im not able to get the image.
– sai
Nov 11 '18 at 16:46
Is this.imgDefault is getting changed onclick?
– Mahi
Nov 11 '18 at 16:51
Yes, It needs to be changed on onclick of the button
– sai
Nov 11 '18 at 17:01
Inspect the div and check whether image is getting loaded on network tab
– Mahi
Nov 11 '18 at 17:03
|
show 4 more comments
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 '18 at 16:13
Thanks for your reply. I tried the same. But Im not able to get the image.
– sai
Nov 11 '18 at 16:46
Is this.imgDefault is getting changed onclick?
– Mahi
Nov 11 '18 at 16:51
Yes, It needs to be changed on onclick of the button
– sai
Nov 11 '18 at 17:01
Inspect the div and check whether image is getting loaded on network tab
– Mahi
Nov 11 '18 at 17:03
|
show 4 more comments
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 '18 at 5:57
answered Nov 11 '18 at 16:10
MahiMahi
745320
745320
Instead of <ng-container>. You should use <div>
– Mahi
Nov 11 '18 at 16:13
Thanks for your reply. I tried the same. But Im not able to get the image.
– sai
Nov 11 '18 at 16:46
Is this.imgDefault is getting changed onclick?
– Mahi
Nov 11 '18 at 16:51
Yes, It needs to be changed on onclick of the button
– sai
Nov 11 '18 at 17:01
Inspect the div and check whether image is getting loaded on network tab
– Mahi
Nov 11 '18 at 17:03
|
show 4 more comments
Instead of <ng-container>. You should use <div>
– Mahi
Nov 11 '18 at 16:13
Thanks for your reply. I tried the same. But Im not able to get the image.
– sai
Nov 11 '18 at 16:46
Is this.imgDefault is getting changed onclick?
– Mahi
Nov 11 '18 at 16:51
Yes, It needs to be changed on onclick of the button
– sai
Nov 11 '18 at 17:01
Inspect the div and check whether image is getting loaded on network tab
– Mahi
Nov 11 '18 at 17:03
Instead of <ng-container>. You should use <div>
– Mahi
Nov 11 '18 at 16:13
Instead of <ng-container>. You should use <div>
– Mahi
Nov 11 '18 at 16:13
Thanks for your reply. I tried the same. But Im not able to get the image.
– sai
Nov 11 '18 at 16:46
Thanks for your reply. I tried the same. But Im not able to get the image.
– sai
Nov 11 '18 at 16:46
Is this.imgDefault is getting changed onclick?
– Mahi
Nov 11 '18 at 16:51
Is this.imgDefault is getting changed onclick?
– Mahi
Nov 11 '18 at 16:51
Yes, It needs to be changed on onclick of the button
– sai
Nov 11 '18 at 17:01
Yes, It needs to be changed on onclick of the button
– sai
Nov 11 '18 at 17:01
Inspect the div and check whether image is getting loaded on network tab
– Mahi
Nov 11 '18 at 17:03
Inspect the div and check whether image is getting loaded on network tab
– Mahi
Nov 11 '18 at 17:03
|
show 4 more comments
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 |
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 |
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 '18 at 18:42
answered Nov 11 '18 at 18:37
RahulRahul
1,0331315
1,0331315
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.
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 '18 at 18:26
@Sai, accept answer if you found one.
– Mahi
Nov 12 '18 at 17:54