【发布时间】:2021-09-19 19:59:10
【问题描述】:
我正在尝试使用从服务返回的信息创建一个表。问题是显示的唯一行是服务返回的列表中的最后一行。
来自list.component.ts的代码
import { Component, OnInit } from '@angular/core';
import { DepartamentoService } from '../../_service/departamento.service';
export interface Items{
idItem: number;
nameItem: string;
}
@Component({
selector: 'app-list',
templateUrl: './list.component.html',
styleUrls: ['./list.component.css']
})
export class LoginComponent implements OnInit {
displayedColumns: string[] = ['idItem', 'nameItem', 'options'];
columnsToDisplay: string[] = this.displayedColumns.slice();
itemList: Items[];
constructor(private itemService: ItemsService) { }
ngOnInit(): void {
this.itemService.list().subscribe(data => {
data.forEach(element => {
this.itemList = [{idItem: element.idItem, nameItem: element.nameItem}];
console.log(`Id: ${element.idItem} - Name ${element.nameItem}`);
});
});
}
}
来自list.component.html的代码
<table mat-table [dataSource]="itemList" class="mat-elevation-z8">
<ng-container [matColumnDef]="column" *ngFor="let column of displayedColumns">
<th id="head" mat-header-cell *matHeaderCellDef> {{column}} </th>
<td mat-cell *matCellDef="let element"> {{element[column]}} </td>
</ng-container>
<tr mat-header-row *matHeaderRowDef="columnsToDisplay"></tr>
<tr mat-row *matRowDef="let row; columns: columnsToDisplay;"></tr>
</table>
感谢任何帮助。
【问题讨论】:
-
什么是 depList ?
-
@pc_coder 是的,我用正确的变量名编辑了问题
标签: arrays angular typescript web-services mat-table