Reputation: 992
I want to loop over an object and display every object in a different tag.
For example, if I have this array:
x = [{'a': 1, 'b': 2}, {'a': 3, 'b': 4}]
I want to display something like this:
first: 1
second: 2
first: 3
second: 4
I tried this:
<ng-container *ngFor="let p of x">
<div>
<label for="recipient-a" class="col-form-label">first:</label>
<input class="form-control" id="recipient-a" type="text" name="a" #name="ngModel" [(ngModel)]="p.a">
</div>
<div>
<label for="recipient-b" class="col-form-label">second:</label>
<input class="form-control" id="recipient-b" type="text" name="b" #name="ngModel" [(ngModel)]="p.b">
</div>
</ng-container>
But this will display only the second element:
first: 3
second: 4
first: 3
second: 4
How can I modify the code to achieve what I want? Thank you for your time!
Upvotes: 1
Views: 57
Reputation: 4533
<ng-container *ngFor="let p of x">
<div>
<label for="recipient-a" class="col-form-label">first:{{p.a}}</label>
<input class="form-control" id="recipient-a" type="text" name="a" #name="ngModel" [(ngModel)]="p.a">
</div>
<div>
<label for="recipient-b" class="col-form-label">second:{{p.b}}</label>
<input class="form-control" id="recipient-b" type="text" name="b" #name="ngModel" [(ngModel)]="p.b">
</div>
Upvotes: 1
Reputation: 4517
Please try this
<ng-container *ngFor="let p of x;">
First: {{p.a}}
<input class="form-control" type="text" [(ngModel)]="p.a">
Second: {{p.b}}
<input class="form-control" type="text" [(ngModel)]="p.b">
</ng-container>
Upvotes: 1