{"id":1051,"date":"2023-06-08T05:00:00","date_gmt":"2023-06-08T12:00:00","guid":{"rendered":"https:\/\/www.angulartraining.com\/daily-newsletter\/?p=1051"},"modified":"2024-10-21T12:32:43","modified_gmt":"2024-10-21T19:32:43","slug":"ngfor-local-variables","status":"publish","type":"post","link":"https:\/\/www.angulartraining.com\/daily-newsletter\/ngfor-local-variables\/","title":{"rendered":"ngFor local variables"},"content":{"rendered":"\n<p>We use the <a href=\"https:\/\/angular.dev\/api\/common\/NgForOf\" target=\"_blank\" rel=\"noopener\" title=\"\"><code>ngFor<\/code> directive<\/a> so often that it&#8217;s easy to forget or even ignore some of its most powerful features.<\/p>\n\n\n\n<p>For instance, it&#8217;s fairly common to access the index of an item in the array we&#8217;re working with, but there are <a href=\"https:\/\/angular.dev\/api\/common\/NgForOf?tab=usage-notes\" target=\"_blank\" rel=\"noopener\" title=\"\">five more local variables<\/a> that can be used:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>index: number<\/code>: The index of the current item in the iterable.<\/li>\n\n\n\n<li><code>count: number<\/code>: The length of the iterable.<\/li>\n\n\n\n<li><code>first: boolean<\/code>: True when the item is the first item in the iterable.<\/li>\n\n\n\n<li><code>last: boolean<\/code>: True when the item is the last item in the iterable.<\/li>\n\n\n\n<li><code>even: boolean<\/code>: True when the item has an even index in the iterable.<\/li>\n\n\n\n<li><code>odd: boolean<\/code>: True when the item has an odd index in the iterable.<\/li>\n<\/ul>\n\n\n\n<p>The syntax to use those local variables is as follows &#8211; and you can use as many as you want in a single <code>ngFor<\/code> directive like so:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"167\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/06\/image-3-1024x167.png\" alt=\"\" class=\"wp-image-1052\" style=\"width:658px;height:107px\" srcset=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/06\/image-3-1024x167.png 1024w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/06\/image-3-300x49.png 300w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/06\/image-3-768x125.png 768w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/06\/image-3.png 1286w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n<\/div>","protected":false},"excerpt":{"rendered":"<p>We use the ngFor directive so often that it&#8217;s easy to forget or even ignore some of its most powerful features. For instance, it&#8217;s fairly common to access the index of an item in the array we&#8217;re working with, but there are five more local variables that can be used: The syntax to use those [&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,7],"tags":[],"class_list":["post-1051","post","type-post","status-publish","format-standard","hentry","category-angular","category-directives","category-template-syntax"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/1051","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=1051"}],"version-history":[{"count":2,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/1051\/revisions"}],"predecessor-version":[{"id":2200,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/1051\/revisions\/2200"}],"wp:attachment":[{"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/media?parent=1051"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/categories?post=1051"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/tags?post=1051"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}