Calling a Component in a Loop in Angular
You can also call the component in a loop, passing it various parameters in attributes. Let's try it in practice. Let's say we have the following property with users:
app/user/user.component.tsinterface User {
name: string,
age: number
}
class UserComponent {
public users: User[] = [
{
name: 'user1',
age: 25,
},
{
name: 'user2',
age: 26,
},
{
name: 'user3',
age: 27,
},
];
}
Let's iterate over the users in a loop, calling the child component with the user in each iteration and passing it the name and age as parameters:
app/app.component.html<div *ngFor="let user in users">
<user-data [name]="user.name" [age]="user.age"></user-data>
</div>
Задание 1
The following array of products is given:
typescriptinterface Product {
name: string,
cost: number
}
class ProductComponent {
public products: Product[] = [
{
name: 'prod1',
cost: 100,
},
{
name: 'prod2',
cost: 200,
},
{
name: 'prod3',
cost: 300,
},
];
}
Loop through this array, creating a new component with a product each time.