{"id":8775,"date":"2023-10-11T09:03:02","date_gmt":"2023-10-11T09:03:02","guid":{"rendered":"https:\/\/www.bacancytechnology.com\/qanda\/?p=8775"},"modified":"2023-10-11T09:03:02","modified_gmt":"2023-10-11T09:03:02","slug":"angular-ngfor-index","status":"publish","type":"post","link":"https:\/\/www.bacancytechnology.com\/qanda\/angular\/angular-ngfor-index","title":{"rendered":"ngFor with Index as Value in Attribute"},"content":{"rendered":"<p>ngFor is a structural directive that renders a template for each item in a collection. The directive is placed on an element, which becomes the parent of the cloned templates. The ngForOf directive is generally used in the shorthand form *ngFor. In this form, the template to be rendered for each iteration is the content of an anchor element containing the directive.<br \/>\nAngular creates an &lt; ng-template &gt; element and applies the *ngFor directive onto it, where it becomes a property binding in square brackets, [ngFor]. The rest of the &lt; div &gt;, including its class attribute, is then moved inside the &lt; ng-template &gt;:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">&lt;ul *ngFor=\"let item of items; let i = index\" data-index=\"i\"&gt;\r\n   &lt;li&gt;{{item}}&lt;\/li&gt;\r\n&lt;\/ul&gt;<\/pre>\n<p>here is how your code will be converted by angular internally<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">&lt;ng-template ngFor let-item [ngForOf]=\"items\" let-i=\"index\"&gt;\r\n   &lt;ul data-index=\"i\"&gt;\r\n       &lt;li&gt;{{item}}&lt;\/li&gt;\r\n   &lt;\/ul&gt;\r\n&lt;\/ng-template&gt;<\/pre>\n<p>In the above code, you can see that the data-index attribute having \u201ci\u201d that will always give you data-index=&#8221;i&#8221; because it is not the attribute of the ul element that\u2019s why the value of i is not acceptable<\/p>\n<p>If you want to store the value of i in the data-index attribute, then you need to make data-index as attribute<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">\/\/for angular 2+\r\n&lt;ul&gt;\r\n   &lt;li  *ngFor=\"let item of items; let i = index\" [attr.data-index]=\"i\"&gt; \r\n       {{item}}\r\n   &lt;\/li&gt;\r\n&lt;\/ul&gt;\r\n<\/pre>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">\/\/for angularJS\r\n&lt;ul&gt;\r\n   &lt;li *ngFor=\"#item of items; #i = index\" [attr.data-index]=\"i\"&gt;\r\n       {{item}}\r\n   &lt;\/li&gt;\r\n&lt;\/ul&gt;\r\n<\/pre>\n<p>And if you add *ngFor on ul, your ul will also be part of the iteration, that\u2019s why I added it on li. <\/p>\n<p>References:<br \/>\n<a href=\"https:\/\/angular.io\/guide\/structural-directives#asterisk\" target=\"_blank\" rel=\"noopener\">angular.io\/guide<\/a><br \/>\n<a href=\"https:\/\/angular.io\/api\/common\/NgFor#description\" target=\"_blank\" rel=\"noopener\">angular.io\/api<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>ngFor is a structural directive that renders a template for each item in a collection. The directive is placed on an element, which becomes the parent of the cloned templates. The ngForOf directive is generally used in the shorthand form *ngFor. In this form, the template to be rendered for each iteration is the content [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":8776,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[9],"tags":[],"class_list":["post-8775","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-angular"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/8775"}],"collection":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/comments?post=8775"}],"version-history":[{"count":2,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/8775\/revisions"}],"predecessor-version":[{"id":8778,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/8775\/revisions\/8778"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/media\/8776"}],"wp:attachment":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/media?parent=8775"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/categories?post=8775"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/tags?post=8775"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}