Google News
logo
Angular - Interview Questions
What Are ngIf and ngFor? Can You Show a Small Example to Use Them?
Just like if and for in other languages, ngIf and ngFor are used as control statements. Example –
<p *ngIf="display">Show this only if the Boolean "display" is true</p>
Where the display is a boolean with the value true or false. Learn more about ngIf.
 
ngFor is used to loop through and display elements of an array (set of data).
<tr *ngFor="let student of students; let i = index"> <td>{{student.name}}

</td> <td>{{i}}</td> </tr>​
The second part (i=index) is optional and only needed if you want to display the index.
Advertisement