{"id":1313,"date":"2023-08-14T05:00:00","date_gmt":"2023-08-14T12:00:00","guid":{"rendered":"https:\/\/www.angulartraining.com\/daily-newsletter\/?p=1313"},"modified":"2023-08-11T19:41:48","modified_gmt":"2023-08-12T02:41:48","slug":"ngswitch-directive","status":"publish","type":"post","link":"https:\/\/www.angulartraining.com\/daily-newsletter\/ngswitch-directive\/","title":{"rendered":"ngSwitch directive"},"content":{"rendered":"\n<p>The <code>ngSwitch<\/code> directive is a good alternative to <code>ngIf<\/code> if you have to handle more than two scenarios.<\/p>\n\n\n\n<p>It works similarly to a <code>switch<\/code> statement in traditional programming languages, allowing you to define a set of cases and associate each case with a specific template block to be rendered. For instance:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/08\/image-14.png\" alt=\"\" class=\"wp-image-1315\" width=\"490\" height=\"232\" srcset=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/08\/image-14.png 928w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/08\/image-14-300x142.png 300w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/08\/image-14-768x364.png 768w\" sizes=\"auto, (max-width: 490px) 100vw, 490px\" \/><\/figure>\n<\/div>\n\n\n<p><\/p>\n\n\n\n<p>One of my favorite use cases is for basic pagination in a component (or an image carousel, for instance) where clicking on navigation buttons changes a value that triggers a different &#8220;case&#8221;:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/08\/image-15.png\" alt=\"\" class=\"wp-image-1316\" width=\"570\" height=\"485\" srcset=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/08\/image-15.png 1016w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/08\/image-15-300x255.png 300w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/08\/image-15-768x653.png 768w\" sizes=\"auto, (max-width: 570px) 100vw, 570px\" \/><\/figure>\n<\/div>\n\n\n<p><\/p>\n\n\n\n<p>Clicking on the buttons changes the value of <code>page<\/code> and displays a different template. Applying this to components instead of HTML elements is especially powerful. For instance: <code>&lt;app-hello *ngSwitchCase=\"3\">&lt;\/app-hello><\/code><\/p>\n\n\n\n<p>You can see an <a href=\"https:\/\/stackblitz.com\/github\/alcfeoh\/ng-training-demos?file=src%2Fapp%2Fngswitch%2Fng-switch.component.ts\" target=\"_blank\" rel=\"noopener\" title=\"\">example in action on Stackblitz here<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The ngSwitch directive is a good alternative to ngIf if you have to handle more than two scenarios. It works similarly to a switch statement in traditional programming languages, allowing you to define a set of cases and associate each case with a specific template block to be rendered. For instance: One of my favorite [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,11],"tags":[],"class_list":["post-1313","post","type-post","status-publish","format-standard","hentry","category-angular","category-directives"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/1313","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/comments?post=1313"}],"version-history":[{"count":2,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/1313\/revisions"}],"predecessor-version":[{"id":1317,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/1313\/revisions\/1317"}],"wp:attachment":[{"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/media?parent=1313"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/categories?post=1313"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/tags?post=1313"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}