{"id":2185,"date":"2024-10-03T14:58:31","date_gmt":"2024-10-03T21:58:31","guid":{"rendered":"https:\/\/www.angulartraining.com\/daily-newsletter\/?p=2185"},"modified":"2024-10-03T14:58:31","modified_gmt":"2024-10-03T21:58:31","slug":"structural-directives-shorthand-syntax","status":"publish","type":"post","link":"http:\/\/www.angulartraining.com\/daily-newsletter\/structural-directives-shorthand-syntax\/","title":{"rendered":"Structural directives shorthand syntax"},"content":{"rendered":"\n<p>Have you ever wondered what&#8217;s behind the following syntax?<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"698\" height=\"72\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2024\/10\/image.png\" alt=\"\" class=\"wp-image-2186\" style=\"width:554px;height:auto\" srcset=\"http:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2024\/10\/image.png 698w, http:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2024\/10\/image-300x31.png 300w\" sizes=\"auto, (max-width: 698px) 100vw, 698px\" \/><\/figure>\n<\/div>\n\n\n<p><\/p>\n\n\n\n<p>Whenever I teach Angular, people end up asking me: &#8220;Why is there a * before <code>ngIf<\/code> and <code>ngFor<\/code>? If I don&#8217;t use the *, the IDE seems to be OK with it.&#8221;<\/p>\n\n\n\n<p>Well, yes and no. If you wanted to remove the *, then you would have to write:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"492\" height=\"139\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2024\/10\/image-1.png\" alt=\"\" class=\"wp-image-2187\" style=\"width:468px;height:auto\" srcset=\"http:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2024\/10\/image-1.png 492w, http:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2024\/10\/image-1-300x85.png 300w\" sizes=\"auto, (max-width: 492px) 100vw, 492px\" \/><\/figure>\n<\/div>\n\n\n<p><\/p>\n\n\n\n<p>In other words, <code>*ngIf<\/code> is a shorthand syntax for <code>&lt;ng-template [ngIf]><\/code>. It&#8217;s designed to make our template syntax more manageable and easier to read.<\/p>\n\n\n\n<p>This is especially true if you use <a href=\"https:\/\/www.angulartraining.com\/daily-newsletter\/ngfor-local-variables\/\" target=\"_blank\" rel=\"noopener\" title=\"\">local variables from exported directive values<\/a>. In those scenarios, the &#8220;long&#8221; syntax is barely readable with the <code>let-<\/code> declarations:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"920\" height=\"375\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2024\/10\/image-2.png\" alt=\"\" class=\"wp-image-2188\" srcset=\"http:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2024\/10\/image-2.png 920w, http:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2024\/10\/image-2-300x122.png 300w, http:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2024\/10\/image-2-768x313.png 768w\" sizes=\"auto, (max-width: 920px) 100vw, 920px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>Note that this syntax trick applies to all directives if you prefix them with *. Here is an <a href=\"https:\/\/stackblitz.com\/edit\/at-dynamic-content-projection-v3?file=src%2Fapp%2Fapp.component.html\" target=\"_blank\" rel=\"noopener\" title=\"\">example where I created my own structural directive to select tab templates<\/a>. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>Have you ever wondered what&#8217;s behind the following syntax? Whenever I teach Angular, people end up asking me: &#8220;Why is there a * before ngIf and ngFor? If I don&#8217;t use the *, the IDE seems to be OK with it.&#8221; Well, yes and no. If you wanted to remove the *, then you would [&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-2185","post","type-post","status-publish","format-standard","hentry","category-angular","category-directives"],"aioseo_notices":[],"_links":{"self":[{"href":"http:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/2185","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/comments?post=2185"}],"version-history":[{"count":1,"href":"http:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/2185\/revisions"}],"predecessor-version":[{"id":2189,"href":"http:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/2185\/revisions\/2189"}],"wp:attachment":[{"href":"http:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/media?parent=2185"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/categories?post=2185"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/tags?post=2185"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}