【问题标题】:Angular Conditional Atribute inside template模板内的角度条件属性
【发布时间】:2021-11-27 20:10:39
【问题描述】:
如何为 HTML 标记提供一个条件属性,该属性根据组件文件中声明的变量的状态进行切换?
我尝试尝试失败:
<th mat-header-cell *matHeaderCellDef {{new ? 'mat-sort-header' : ''}} >
在这种情况下,由于 *matHeaderCellDef 引用,不适合使用 *ngIf 或 ng-template。
谢谢。
【问题讨论】:
标签:
html
node.js
angular
typescript
angular-material
【解决方案1】:
您可以尝试以下方法来做到这一点-
通过应用 *ngIf 指令
<th mat-header-cell *matHeaderCellDef *ngIf="!new" >
<th mat-header-cell *matHeaderCellDef mat-sort-header *ngIf="new" >
通过使用属性绑定-
<th mat-header-cell *matHeaderCellDef [attr.mat-sort-header]="new?'': null" >
确保我从您的示例代码中获取的“new”是变量,它存储表达式的值以确定它是否必须使用 mat-sort-header 属性。
还有一个提示,你不能在 javascript 中使用 new 作为变量名作为其保留关键字。