【发布时间】:2020-07-16 10:02:19
【问题描述】:
我有以下代码,我希望每当列表为空时td 显示一个文本,并且一旦它获得一些元素,就显示element.name 但它仅在列表不为空时才有效(我使用console.log(list) 确保长度为 0)
<table mat-table [dataSource]="list" class=" w-100">
<ng-container matColumnDef="name">
<th mat-header-cell *matHeaderCellDef>header</th>
<td mat-cell *matCellDef="let element">
{{ list.length ? element.name : 'the list is empty' }}
</td>
</ng-container>
</table>
问题是,当列表为空时,什么都不会显示,但是只要我在列表中添加一些东西,我就可以看到我添加的元素的名称。
我还尝试在 div 中使用 ngIf,然后使用 else 语句添加另一个 ng-template。
【问题讨论】:
-
@LuísRamalho 这不对。 list.length 等于 0 时是假的
标签: javascript html angular angular-material conditional-operator