简体   繁体   中英

loop thru array and display data in angular

enter image description here I am very new to angular and I want to loop throu an array in angular. My array har these elements in it.

"ticketsdetectives":[10,11,12,13]

Now I want to to display this data. So something like this :

for(int i=0; i<ticketdetectives.size(); i++){ 
         printf("%d",ticketdetectives[i]);
}

My code in angular is like this

<mat-form-field appearance="fill" *ngFor="let string of ticketNames">
                <mat-label>{{string}} tickets:</mat-label>
                <input matInput type="text" pattern="([0-9]+\s*[,]\s*)*[0-9]+" formControlName="ticketsdetectives"/>
</mat-form-field>

ticketNames is just an array of strings. How do I loop thru my array and display data in input. right now my data is displayed as array and I want it to be displayed as elements.

可能只是您定义的数组在打字稿文件中具有不同的名称“ticketsdetectives”,而您在 html 模板中使用了“ticketNames”

Create a local variable in your .ts:

tickets = [{name:'taxi', detective: 10}, {name: 'bus', detective:11 }, { name: 'underground',detective: 12 },{ name: 'boat', detective 13 }];

In your html

<mat-form-field appearance="fill" *ngFor="let ticket of tickets">
                <mat-label>{{ ticket.name}} tickets:</mat-label>
                <input [ngModel]="ticket.detective" matInput type="text" pattern="([0-9]+\s*[,]\s*)*[0-9]+"/>
</mat-form-field>

If you want to use formControlName please see the docs here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM