Bacancy Technology
Bacancy Technology represents the connected world, offering innovative and customer-centric information technology experiences, enabling Enterprises, Associates and the Society to Rise™.
12+
Countries where we have happy customers
1050+
Agile enabled employees
06
World wide offices
12+
Years of Experience
05
Agile Coaches
14
Certified Scrum Masters
1000+
Clients projects
1458
Happy customers
Artificial Intelligence
Machine Learning
Salesforce
Microsoft
SAP
October 11, 2023
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.
Angular creates an < ng-template > element and applies the *ngFor directive onto it, where it becomes a property binding in square brackets, [ngFor]. The rest of the < div >, including its class attribute, is then moved inside the < ng-template >:
<ul *ngFor="let item of items; let i = index" data-index="i"> <li>{{item}}</li> </ul>
here is how your code will be converted by angular internally
<ng-template ngFor let-item [ngForOf]="items" let-i="index"> <ul data-index="i"> <li>{{item}}</li> </ul> </ng-template>
In the above code, you can see that the data-index attribute having “i” that will always give you data-index=”i” because it is not the attribute of the ul element that’s why the value of i is not acceptable
If you want to store the value of i in the data-index attribute, then you need to make data-index as attribute
//for angular 2+ <ul> <li *ngFor="let item of items; let i = index" [attr.data-index]="i"> {{item}} </li> </ul>
//for angularJS <ul> <li *ngFor="#item of items; #i = index" [attr.data-index]="i"> {{item}} </li> </ul>
And if you add *ngFor on ul, your ul will also be part of the iteration, that’s why I added it on li.
References:
angular.io/guide
angular.io/api