Different between *ngIf vs [ngSwitch] in Angular 2+
What are the differences between [ngSwitch]
and a bunch of *ngIf
s. 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
add a comment |
What are the differences between [ngSwitch]
and a bunch of *ngIf
s. 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
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
add a comment |
What are the differences between [ngSwitch]
and a bunch of *ngIf
s. 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
What are the differences between [ngSwitch]
and a bunch of *ngIf
s. 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
angular ngif ng-switch
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
add a comment |
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
add a comment |
3 Answers
3
active
oldest
votes
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.
add a comment |
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.
add a comment |
*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
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%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
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.
add a comment |
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.
add a comment |
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.
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.
answered Jan 2 at 3:44
Gourav SinglaGourav Singla
313
313
add a comment |
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
edited Jan 2 at 6:43
Tushar Walzade
2,38131735
2,38131735
answered Jan 2 at 4:14
Yasir ArafatYasir Arafat
21717
21717
add a comment |
add a comment |
*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
add a comment |
*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
add a comment |
*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
*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
answered Jan 2 at 4:14
Akshay RajputAkshay Rajput
67627
67627
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%2f54000683%2fdifferent-between-ngif-vs-ngswitch-in-angular-2%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
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