⊗jsagPmCMLC 64 of 97 menu

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:

interface 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:

<div *ngFor="let user in users"> <user-data [name]="user.name" [age]="user.age"></user-data> </div>

The following array of products is given:

interface 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.

English
AfrikaansAzərbaycanБългарскиবাংলাБеларускаяČeštinaDanskDeutschΕλληνικάEspañolEestiSuomiFrançaisहिन्दीMagyarՀայերենIndonesiaItaliano日本語ქართულიҚазақ한국어КыргызчаLietuviųLatviešuМакедонскиMelayuမြန်မာNederlandsNorskPolskiPortuguêsRomânăРусскийසිංහලSlovenčinaSlovenščinaShqipСрпскиSrpskiSvenskaKiswahiliТоҷикӣไทยTürkmenTürkçeЎзбекOʻzbekTiếng Việt
We use cookies for website operation, analytics, and personalization. Data processing is carried out in accordance with the Privacy Policy.
accept all customize decline