The 'ngFor' is a built-in directive in Angular, a popular web development framework, that allows us to repeat a particular template for each item present in a list or an array. The 'ngFor' directive is often termed as a "repeater" directive because it can create copies of HTML elements and generate a dynamic list.
Consider this basic example:
<ul>
<li *ngFor="let item of items">
{{ item }}
</li>
</ul>
In the above code, *ngFor
directive is used with a li
element. The "items" is an array and 'item' represents each element in the array 'items'. For every item in the 'items' array, the 'ngFor' directive will make a copy of the 'li' element and insert it into the DOM.
Note the use of the "let" keyword - it's used to define a local variable in the template view. In this case, 'item' is the local variable that represents the current item from the 'items' array. The expression within the double-curlies '{{ }}' is called a template expression - in this case, it's used to display the value of each 'item' in the array.
The result is a dynamic list where each item in the 'items' is displayed as a separate list item in a bullet point list.