Here's an example of how to use ngFor in a <ul> tag to display a list of items:
html<ul>
<li *ngFor="let item of items">{{ item }}</li>
</ul>
To illustrate the output of the above example, let's assume the component class has the following items array:
When the above code is rendered, the <ul> tag with the *ngFor directive will generate a list with three <li> tags, each containing one item from the `items` array:
typescriptexport class MyComponent {
items: string[] = ['Apple', 'Banana', 'Orange'];
}
html<ul>
<li>Apple</li>
<li>Banana</li>
<li>Orange</li>
</ul>
Assuming the items array in the component class contains ['Apple', 'Banana', 'Orange'], the browser will render the following output:
Apple
Banana
Orange
Banana
Orange
By utilizing ngFor, you can dynamically generate HTML content based on the items in an array or iterable object, allowing you to create lists, tables, or any other repeating elements in your Angular templates.
No comments:
Post a Comment