我有帶選項選擇的墊子表單欄位
<mat-form-field appearance="outline">
<mat-label>TestMat</mat-label>
<mat-select placeholder="TestMat" [formControl]="testControl">
<mat-option *ngFor="let arr of arrTmp" [value]="arr">
{{arr.name}}
</mat-option>
</mat-select>
</mat-form-field>
其中 arrTmp =
[
[{
"name": "name1",
"id": "1"
}],
[{
"name": "name2",
"id": "2"
}, {
"name": "name3",
"id": "2"
}]
]
結果在墊子表單欄位內我有
[object Object]
[object Object],[object Object]
我如何才能正常顯示 arr.name?
uj5u.com熱心網友回復:
您需要為嵌套陣列使用一個額外的 for 回圈,請參閱下面的代碼
<mat-form-field appearance="outline">
<mat-label>TestMat</mat-label>
<mat-select placeholder="TestMat" [formControl]="testControl">
<ng-container *ngFor="let arr of arrTmp">
<mat-option *ngFor="let item of arr" [value]="item">
{{item.name}}
</mat-option>
</ng-container>
</mat-select>
</mat-form-field>
uj5u.com熱心網友回復:
您可以添加一個元素,如跨度,在第一個陣列上添加一個 ngFor 回圈,然后使用您當前的 ngFor 對嵌套陣列進行 foop。
所以這意味著做類似的事情:
<mat-form-field appearance="outline">
<mat-label>TestMat</mat-label>
<mat-select placeholder="TestMat" [formControl]="testControl">
<span *ngFor="let mainArr of arrTmp">
<mat-option *ngFor="let arr of mainArr" [value]="arr">
{{arr.name}}
</mat-option>
</span>
</mat-select>
</mat-form-field>
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/326547.html
上一篇:禁用angular2多選復選框
