我想制作一个循环来显示我的角度材料表中的一些数据,但我无法做到这一点,因为我不知道如何做。
// <img src=" assets/{{element[col.key][0].position}}.jpg alt="img"> // this way works out of the loop.
ts.file
columns = [
{key: 'position', display ...., config : {isPosition:true}},
];
材质.表.组件
<ng-container *ngFor="let col of columns">
<ng-container *ngIf="col.config.isPosition">
<div *ngFor="let element of col.config.isPosition">
<img src=" assets/{{element[col.key].position}}.jpg alt="img">
</div>
</ng-container>
</ng-container>
您不能将
*ngFor
用于有角度的材料表。你必须遵守实施规则。
您必须在 .ts 文件中定义数据,如下所示,然后您可以将其绑定到 tr,如 *matRowDef="let row; columns: displayedColumns;"
。
这是来自 Angular Material 官方网站的示例,可在 here 中找到。
HTML:
<table mat-table [dataSource]="dataSource" class="mat-elevation-z8">
<ng-container matColumnDef="position">
<th mat-header-cell *matHeaderCellDef> No. </th>
<td mat-cell *matCellDef="let element"> {{element.position}} </td>
</ng-container>
<ng-container matColumnDef="name">
<th mat-header-cell *matHeaderCellDef> Name </th>
<td mat-cell *matCellDef="let element"> {{element.name}} </td>
</ng-container>
<ng-container matColumnDef="weight">
<th mat-header-cell *matHeaderCellDef> Weight </th>
<td mat-cell *matCellDef="let element"> {{element.weight}} </td>
</ng-container>
<ng-container matColumnDef="symbol">
<th mat-header-cell *matHeaderCellDef> Symbol </th>
<td mat-cell *matCellDef="let element"> {{element.symbol}} </td>
</ng-container>
<tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
<tr mat-row *matRowDef="let row; columns: displayedColumns;"></tr>
</table>
TS:
import {Component} from '@angular/core';
export interface PeriodicElement {
name: string;
position: number;
weight: number;
symbol: string;
}
const ELEMENT_DATA: PeriodicElement[] = [
{position: 1, name: 'Hydrogen', weight: 1.0079, symbol: 'H'},
{position: 2, name: 'Helium', weight: 4.0026, symbol: 'He'},
{position: 3, name: 'Lithium', weight: 6.941, symbol: 'Li'},
{position: 4, name: 'Beryllium', weight: 9.0122, symbol: 'Be'},
{position: 5, name: 'Boron', weight: 10.811, symbol: 'B'},
{position: 6, name: 'Carbon', weight: 12.0107, symbol: 'C'},
{position: 7, name: 'Nitrogen', weight: 14.0067, symbol: 'N'},
{position: 8, name: 'Oxygen', weight: 15.9994, symbol: 'O'},
{position: 9, name: 'Fluorine', weight: 18.9984, symbol: 'F'},
{position: 10, name: 'Neon', weight: 20.1797, symbol: 'Ne'},
];
@Component({
selector: 'table-basic-example',
styleUrls: ['table-basic-example.css'],
templateUrl: 'table-basic-example.html',
})
export class TableBasicExample {
displayedColumns: string[] = ['position', 'name', 'weight', 'symbol'];
dataSource = ELEMENT_DATA;
}
为了实现
img
标签,您可以创建一个包含图像 src 的对象,并将其映射到自己的列,然后在 DOM 中使用该对象。
在线程上有点太晚了,但是,我能够在我的应用程序上解决这个类似的问题。与第一个答案相反,我们可以在 ng 表上使用 *ngFor。
<table mat-table [dataSource]="materials">
<ng-container *ngFor="let col of columns;" [matColumnDef]="col">
<th mat-header-cell *matHeaderCellDef> {{ col | uppercase }} </th>
<td mat-cell *matCellDef="let material"> {{ material[col] }} </td>
</ng-container>
<tr mat-header-row *matHeaderRowDef="columns"></tr>
<tr mat-row *matRowDef="let row; columns: columns;"></tr>
</table>
代码中的
material[col]
部分是这个问题的关键部分。基本上,如果我们尝试访问 material
,它就是数组的单个 json。该 json 包含我们希望在 columns
中定义的每个属性。要访问 json 的每个属性,我们只需要调用 json[propertyNameInString]
,在我们的例子中是 material[col]
。