Angular 模板内的条件属性
Angular Conditional Atribute inside template
如何给 HTML 标签一个条件属性,该属性根据组件文件中声明的变量的状态进行切换?
我试过没有成功:
<th mat-header-cell *matHeaderCellDef {{new ? 'mat-sort-header' : ''}} >
在这种情况下,由于 *matHeaderCellDef 引用,使用 *ngIf 或 ng-template 是不合适的。
谢谢。
您可以尝试以下方法-
通过应用 *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 属性或不是。
另一个提示你不能使用 new 作为变量名作为它在 javascript.
中的保留关键字
如何给 HTML 标签一个条件属性,该属性根据组件文件中声明的变量的状态进行切换? 我试过没有成功:
<th mat-header-cell *matHeaderCellDef {{new ? 'mat-sort-header' : ''}} >
在这种情况下,由于 *matHeaderCellDef 引用,使用 *ngIf 或 ng-template 是不合适的。
谢谢。
您可以尝试以下方法-
通过应用 *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 属性或不是。 另一个提示你不能使用 new 作为变量名作为它在 javascript.
中的保留关键字