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?










share|improve this question























  • 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














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?










share|improve this question























  • 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












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?










share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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
















  • 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












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






share|improve this answer






















  • 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

















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 !!






share|improve this answer






















    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%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








    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






    share|improve this answer






















    • 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














    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






    share|improve this answer






















    • 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












    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






    share|improve this answer














    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







    share|improve this answer














    share|improve this answer



    share|improve this answer








    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
















    • 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












    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 !!






    share|improve this answer


























      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 !!






      share|improve this answer
























        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 !!






        share|improve this answer














        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 !!







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 11 at 18:42

























        answered Nov 11 at 18:37









        Rahul

        9381315




        9381315



























            draft saved

            draft discarded
















































            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.




            draft saved


            draft discarded














            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





















































            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







            這個網誌中的熱門文章

            What does pagestruct do in Eviews?

            Dutch intervention in Lombok and Karangasem

            Channel Islands