Different between *ngIf vs [ngSwitch] in Angular 2+












2















What are the differences between [ngSwitch] and a bunch of *ngIfs. Any performance factors we should be concerned about?



*ngIf



  <div *ngIf="day === 'MONDAY'">
Keep calm and pretend it's not Monday.
</div>
...
<div *ngIf="day === 'FRIDAY'">
Happy Friday!
</div>


[ngSwitch]



<ng-container [ngSwitch]="day">

<div *ngSwitchCase="'MONDAY'">
Keep calm and pretend it's not Monday.
</div>
...
<div *ngSwitchCase="'FRIDAY'">
Happy Friday!
</div>

</ng-container>









share|improve this question


















  • 1





    codecraft.tv/courses/angular/built-in-directives/…

    – RN92
    Jan 2 at 3:16











  • similar using angular js - stackoverflow.com/questions/16741949/…

    – Tushar Walzade
    Jan 2 at 6:34
















2















What are the differences between [ngSwitch] and a bunch of *ngIfs. Any performance factors we should be concerned about?



*ngIf



  <div *ngIf="day === 'MONDAY'">
Keep calm and pretend it's not Monday.
</div>
...
<div *ngIf="day === 'FRIDAY'">
Happy Friday!
</div>


[ngSwitch]



<ng-container [ngSwitch]="day">

<div *ngSwitchCase="'MONDAY'">
Keep calm and pretend it's not Monday.
</div>
...
<div *ngSwitchCase="'FRIDAY'">
Happy Friday!
</div>

</ng-container>









share|improve this question


















  • 1





    codecraft.tv/courses/angular/built-in-directives/…

    – RN92
    Jan 2 at 3:16











  • similar using angular js - stackoverflow.com/questions/16741949/…

    – Tushar Walzade
    Jan 2 at 6:34














2












2








2








What are the differences between [ngSwitch] and a bunch of *ngIfs. Any performance factors we should be concerned about?



*ngIf



  <div *ngIf="day === 'MONDAY'">
Keep calm and pretend it's not Monday.
</div>
...
<div *ngIf="day === 'FRIDAY'">
Happy Friday!
</div>


[ngSwitch]



<ng-container [ngSwitch]="day">

<div *ngSwitchCase="'MONDAY'">
Keep calm and pretend it's not Monday.
</div>
...
<div *ngSwitchCase="'FRIDAY'">
Happy Friday!
</div>

</ng-container>









share|improve this question














What are the differences between [ngSwitch] and a bunch of *ngIfs. Any performance factors we should be concerned about?



*ngIf



  <div *ngIf="day === 'MONDAY'">
Keep calm and pretend it's not Monday.
</div>
...
<div *ngIf="day === 'FRIDAY'">
Happy Friday!
</div>


[ngSwitch]



<ng-container [ngSwitch]="day">

<div *ngSwitchCase="'MONDAY'">
Keep calm and pretend it's not Monday.
</div>
...
<div *ngSwitchCase="'FRIDAY'">
Happy Friday!
</div>

</ng-container>






angular ngif ng-switch






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Jan 2 at 2:48









Manoj ShresthaManoj Shrestha

1,47612035




1,47612035








  • 1





    codecraft.tv/courses/angular/built-in-directives/…

    – RN92
    Jan 2 at 3:16











  • similar using angular js - stackoverflow.com/questions/16741949/…

    – Tushar Walzade
    Jan 2 at 6:34














  • 1





    codecraft.tv/courses/angular/built-in-directives/…

    – RN92
    Jan 2 at 3:16











  • similar using angular js - stackoverflow.com/questions/16741949/…

    – Tushar Walzade
    Jan 2 at 6:34








1




1





codecraft.tv/courses/angular/built-in-directives/…

– RN92
Jan 2 at 3:16





codecraft.tv/courses/angular/built-in-directives/…

– RN92
Jan 2 at 3:16













similar using angular js - stackoverflow.com/questions/16741949/…

– Tushar Walzade
Jan 2 at 6:34





similar using angular js - stackoverflow.com/questions/16741949/…

– Tushar Walzade
Jan 2 at 6:34












3 Answers
3






active

oldest

votes


















1














ngIf is basically a version of ngSwitch with a single condition. It's different from ngShow in that it removes the actual DOM element rather than simply hiding it. If you're using an ngSwitch with just a singly truthy condition check, then I believe ngIf will do the same thing.






share|improve this answer































    1














    For *ngIf, every condition will be checked and the code inside the true condition will be executed.



    For [ngSwitch], only the code inside the specific case will be executed (using break;).



    So, [ngSwitch] will be faster where there are multiple cases.






    share|improve this answer

































      0














      *ngIf works like if statement and ngSwitch (actually comprised of two directives, an attribute directive, and a structural directive) work as a switch statement in the DOM.



      Knowing the difference between if-else statement and switch cases will help you understand further,
      https://techdifferences.com/difference-between-if-else-and-switch.html






      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',
        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
        });


        }
        });














        draft saved

        draft discarded


















        StackExchange.ready(
        function () {
        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54000683%2fdifferent-between-ngif-vs-ngswitch-in-angular-2%23new-answer', 'question_page');
        }
        );

        Post as a guest















        Required, but never shown

























        3 Answers
        3






        active

        oldest

        votes








        3 Answers
        3






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        1














        ngIf is basically a version of ngSwitch with a single condition. It's different from ngShow in that it removes the actual DOM element rather than simply hiding it. If you're using an ngSwitch with just a singly truthy condition check, then I believe ngIf will do the same thing.






        share|improve this answer




























          1














          ngIf is basically a version of ngSwitch with a single condition. It's different from ngShow in that it removes the actual DOM element rather than simply hiding it. If you're using an ngSwitch with just a singly truthy condition check, then I believe ngIf will do the same thing.






          share|improve this answer


























            1












            1








            1







            ngIf is basically a version of ngSwitch with a single condition. It's different from ngShow in that it removes the actual DOM element rather than simply hiding it. If you're using an ngSwitch with just a singly truthy condition check, then I believe ngIf will do the same thing.






            share|improve this answer













            ngIf is basically a version of ngSwitch with a single condition. It's different from ngShow in that it removes the actual DOM element rather than simply hiding it. If you're using an ngSwitch with just a singly truthy condition check, then I believe ngIf will do the same thing.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Jan 2 at 3:44









            Gourav SinglaGourav Singla

            313




            313

























                1














                For *ngIf, every condition will be checked and the code inside the true condition will be executed.



                For [ngSwitch], only the code inside the specific case will be executed (using break;).



                So, [ngSwitch] will be faster where there are multiple cases.






                share|improve this answer






























                  1














                  For *ngIf, every condition will be checked and the code inside the true condition will be executed.



                  For [ngSwitch], only the code inside the specific case will be executed (using break;).



                  So, [ngSwitch] will be faster where there are multiple cases.






                  share|improve this answer




























                    1












                    1








                    1







                    For *ngIf, every condition will be checked and the code inside the true condition will be executed.



                    For [ngSwitch], only the code inside the specific case will be executed (using break;).



                    So, [ngSwitch] will be faster where there are multiple cases.






                    share|improve this answer















                    For *ngIf, every condition will be checked and the code inside the true condition will be executed.



                    For [ngSwitch], only the code inside the specific case will be executed (using break;).



                    So, [ngSwitch] will be faster where there are multiple cases.







                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Jan 2 at 6:43









                    Tushar Walzade

                    2,38131735




                    2,38131735










                    answered Jan 2 at 4:14









                    Yasir ArafatYasir Arafat

                    21717




                    21717























                        0














                        *ngIf works like if statement and ngSwitch (actually comprised of two directives, an attribute directive, and a structural directive) work as a switch statement in the DOM.



                        Knowing the difference between if-else statement and switch cases will help you understand further,
                        https://techdifferences.com/difference-between-if-else-and-switch.html






                        share|improve this answer




























                          0














                          *ngIf works like if statement and ngSwitch (actually comprised of two directives, an attribute directive, and a structural directive) work as a switch statement in the DOM.



                          Knowing the difference between if-else statement and switch cases will help you understand further,
                          https://techdifferences.com/difference-between-if-else-and-switch.html






                          share|improve this answer


























                            0












                            0








                            0







                            *ngIf works like if statement and ngSwitch (actually comprised of two directives, an attribute directive, and a structural directive) work as a switch statement in the DOM.



                            Knowing the difference between if-else statement and switch cases will help you understand further,
                            https://techdifferences.com/difference-between-if-else-and-switch.html






                            share|improve this answer













                            *ngIf works like if statement and ngSwitch (actually comprised of two directives, an attribute directive, and a structural directive) work as a switch statement in the DOM.



                            Knowing the difference between if-else statement and switch cases will help you understand further,
                            https://techdifferences.com/difference-between-if-else-and-switch.html







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Jan 2 at 4:14









                            Akshay RajputAkshay Rajput

                            67627




                            67627






























                                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.




                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function () {
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54000683%2fdifferent-between-ngif-vs-ngswitch-in-angular-2%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







                                Popular posts from this blog

                                'app-layout' is not a known element: how to share Component with different Modules

                                android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

                                WPF add header to Image with URL pettitions [duplicate]