【发布时间】:2020-05-27 12:29:00
【问题描述】:
在我的 JSON 中有多条记录,但在浏览器中它只显示一条记录。我尝试使用 NgFor 重复相同的操作,但出现错误。
这是我的第一个孩子 html
<ng-container matColumnDef="organization">
<th mat-header-cell *matHeaderCellDef mat-sort-header> Organization Name </th>
<td mat-cell *matCellDef="let element" (click)="showInfo(element)"> {{element.organizationName}}
</td>
</ng-container>
这是第一个孩子的 ts
showInfo(rowData) {
let dialogConfig = new MatDialogConfig();
dialogConfig = {
width: '850px',
data:rowData
}
const dialogRef = this.dialog.open(ModalPopupTrialTypeComponent, dialogConfig);
}
这是我的 JSON:
[
{
"regionName": "EMEA",
"regionCurrency": "USD",
"orgnazationName": "XYZ",
"orgnazationSubName": "Miller"
"Department": [
{
"DepartmentName": "Main",
"FirstName": "David",
"LastName": "Brown",
"Band": 2,
"Salary": 10000.00
},
{
"DepartmentName": "Main 1",
"FirstName": "Marry",
"LastName": "Brown",
"Band": 2,
"Salary": 10000.00
}
]
}
]
这是我的 ts 代码:
export class ModalPopupTrialTypeComponent implements OnInit {
public rowData: any;
private unsubscribe: Subscription[] = [];
constructor(
private dialogRef: MatDialogRef<ModalPopupTrialTypeComponent>,
@Inject(MAT_DIALOG_DATA) public Modaldata) {
this.rowData = Modaldata;
}
ngOnInit() {
console.log('Modal Data',this.rowData);
}
ngOnDestroy() {
this.unsubscribe.forEach(sb => sb.unsubscribe());
}
}
这是我的 HTML 代码
<table style="width: 100%;" >
<thead>
<tr>
<th>Department Name</th>
<th>First Name</th>
<th>Last Name</th>
<th>Band</th>
<th>Salary</th>
</tr>
</thead>
<tbody>
<tr *ngFor="let item of rowData.Department" >
<td>{{item.DepartmentName}}</td>
<td>{{item.FirstName}}</td>
<td>{{item.LastName}}</td>
<td>{{item.Band}}</td>
<td>{{item.Salary}}</td>
</tr >
</tbody>
</table>
我收到以下错误:
找不到“object”类型的不同支持对象“[object Object]”。 NgFor 只支持绑定到 Arrays 等 Iterables。
在 NGFor 中添加 .department 后,我没有收到错误,但它只显示一条记录而不是多条记录
【问题讨论】:
-
你试过
*ngFor="let item of rowData.Department。看起来这就是您正在尝试做的事情 -
@JorgeMussato 是的,我已经尝试过了,我没有收到错误,但没有收到多个值
-
尝试在表格标签之外添加
{{rowData | json}}并在此处发布屏幕上显示的内容。因为您帖子中的JSON可能是错误的