angular - 角度如何在标签内使用 If .. then.. else 条件实现 *ngIf?
问题描述
我尝试使用 ngIf 在表块内显示 if else 条件,但我无法显示它。
<tr *ngFor="let client of clients">
<!--here i need to implement it-->
<td *ngIf="!client.auditorGroup"; then
[ngStyle]="titleStyles else [ngStyle]="oldStyles1">{{client.selectionId}}</td>
<td>{{client.selectionDate}}</td>
<td>{{client.selectedBy}}</td>
<td>{{client.panEximNumber}}</td>
<td>{{client.name}}</td>
<td>{{client.address}}</td>
<td>{{client.phoneNumber}}</td>
<td>{{client.selectionType}}</td>
<td *ngIf="!client.auditorGroup" [ngStyle]="titleStyles">Edit
Delete</td>
</tr>
解决方案
您需要提供对模板的引用:
<table>
<tr>
<td *ngIf="show; else tpl">A</td>
</tr>
</table>
<button (click)="show = !show">Toggle</button>
<ng-template #tpl><td>Else template</td></ng-template>
如果要根据条件切换样式,可以使用以下方法:
<td [ngStyle]="calcStyles(client.auditorGroup)">{{client.selectionId}}</td>
calcStyles(auditorGroup) {
if(auditorGroup) {
return { color: 'red' }
}
return { color: 'green' }
}
推荐阅读
- c# - asp.net 中的 C# 数据列表
- .net-core - 应用洞察 | 有时端到端交易详细信息不会显示所有遥测数据
- google-chrome - 在注册表中强制执行隐身模式时,Selenium 无法启动 Chrome 89
- inheritance - 通过继承和多态重构代码
- android - 有没有办法查明是否显示了 Google In App Review?
- reactjs - 我正在尝试从其他服务器加载 PDF 文件作为 URL,加载需要更多时间
- javascript - 在 React/JSX 中显示嵌套的 JSON
- gcc - rust msp430 链接器错误 - .text 不在区域 ROM 内
- html - 在新创建的 ASP.NET 项目中,为什么 _LoginPartial 放在右边,而没有任何标记指定这样的位置?
- typescript - TypeScript 泛型 - 以实现类对象作为值有条件地设置泛型接口类型的参数