【发布时间】:2019-07-13 00:16:10
【问题描述】:
我正在尝试将不同的角度类布尔属性分配给 ngIf,但我使用模板检查了 ngSwitch 和 ngIf else,我认为这些不会起作用。
在我的示例数据集中我有这样的东西
dataSet: [ { total: '$382734.99', clauseName: 'showTotalDollars' },
{ total: '3.22%', clauseName: 'showPercentageData' }
]
我的类布尔属性定义为
showTotalDollars: boolean = true;
showPercentageData: boolean = false;
在我的角度代码中,我尝试了以下不起作用的方法
<div class="ng-container" *ngFor="let dataset of sales.dataset">
<div class="ng-container" *ngIf="{{dataset.clauseName}}">
<mat-list-item><span>{{total}}</span></mat-list-item>
</div>
</div>
ngIf does not work with interpolation.
I also tried
*ngIf="getClauseName(item)"
In my ts code
getClause(clickedItem: string) {
if (clickedItem.clauseName == 'showTotalDollars') {
return 'showTotalDollars;
}
else if (clickedItem.clauseName == 'showPercentageData') {
return 'showPercentageData;
}
... and so on
}
这也不起作用。
It seems like in the ngIf works with *ngIf="showTotalDollars" and *ngIf="showPercentageData" in the html code and not getting assigned the name of the boolean properties in the ts code.
当角度代码循环遍历数据集中的每条记录并检查clauseName(有很多,而不仅仅是2个)并分配类布尔属性名称时,我需要这样的东西。像这样工作的东西
if (clauseName == 'showTotalDollars') then
*ngIf="showTotalDollars";
else if (clauseName == 'showPercentageData') then
*ngIf="showPercentageData";
玩了插值和getClauseName函数后,我不知道如何实现。
期望的行为是 ngFor 循环遍历数据集中的每条记录(有很多),并检查提供数据类型指示的子句名称。找到一种方法将 ngIf 动态设置为 ts 类中的特定布尔属性名称。
感谢任何帮助。谢谢。
【问题讨论】:
-
你能创建一个这个例子的stackblitz吗
-
你要检查的条件是什么
标签: angular typescript