【发布时间】:2018-12-25 19:46:04
【问题描述】:
我试图显示从本地 API 检索到的简单数据,我使用 mat-table 存储在数组中,但无济于事。我设法修复了它,但我对 Angular/Typescript/Programming 很陌生,我不知道为什么我的修复有效,有人可以帮我理解为什么会这样吗?
pago.component.ts(修复前)
export class PagosComponent implements OnInit {
facturasElectricas: FacturaE[] = [];
displayedColumns: string[] = ['fecha', 'monto'];
...
ngOnInit() {
this.getFacturasElectricas();
}
getFacturasElectricas(): void {
const id = +this.route.snapshot.paramMap.get('id');
this.pagoService.getFacturasElectricas().subscribe(facturas => {
facturas.forEach(f => {
if (f.factura.contrato === id && !f.factura.pagado) {
this.facturasElectricas.push(f);
}
});
});
}
}
pago.component.ts(修复后)
export class PagosComponent implements OnInit {
facturasElectricas: FacturaE[];
displayedColumns: string[] = ['fecha', 'monto'];
...
ngOnInit() {
this.getFacturasElectricas();
}
getFacturasElectricas(): void {
const id = +this.route.snapshot.paramMap.get('id');
this.pagoService.getFacturasElectricas().subscribe(facturas => {
this.facturasElectricas = [];
facturas.forEach(f => {
if (f.factura.contrato === id && !f.factura.pagado) {
this.facturasElectricas.push(f);
}
});
});
}
}
我唯一更改的行是this.facturasElectricas = [];,将它放在getFacturasElectricas() 方法中。
这是html部分 pago.component.html
<table *ngIf="facturasElectricas" mat-table #table [dataSource]="facturasElectricas">
<ng-container matColumnDef="fecha">
<th mat-header-cell *matHeaderCellDef> Fecha </th>
<td mat-cell *matCellDef="let element"> {{element.factura.fecha}} </td>
</ng-container>
<ng-container matColumnDef="monto">
<th mat-header-cell *matHeaderCellDef> Debe </th>
<td mat-cell *matCellDef="let element"> {{element.monto}}</td>
</ng-container>
<tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
<tr mat-row *matRowDef="let row; columns: displayedColumns;"></tr>
</table>
【问题讨论】:
标签: angular typescript material-design