我想遍历一个对象,并在不同的标签中显示每个对象。
例如,如果我有此数组:
x = [{'a': 1, 'b': 2}, {'a': 3, 'b': 4}]
我想显示以下内容:
first: 1
second: 2
first: 3
second: 4
我尝试过:
<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>
但这将仅显示第二个元素:
first: 3
second: 4
first: 3
second: 4
如何修改代码以实现我想要的?谢谢您的时间!
答案 0 :(得分:1)
请尝试
<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>
答案 1 :(得分:1)
<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>