我正在尝试在我的 Angular 应用程序中的 mat-table 的 mat-cell 内实现 if else 条件。但我从控制台收到错误消息“错误错误:StaticInjectorError(AppModule)[NgIf -> TemplateRef]:”
我的代码是
<ng-container matColumnDef="role">
<mat-header-cell *matHeaderCellDef>Role</mat-header-cell>
<mat-cell *matCellDef="let user" ngIf="{{user.roles.length == 1}}">
Admin
</mat-cell>
</ng-container>
非常感谢任何帮助。
最佳答案
你有ngIf
, 但它应该以星号为前缀:*ngIf
此外,还有一个绑定(bind)指令属性,如 *ngIf
您不需要在其中使用花括号。只是在做*ngIf="user.roles.length == 1"
应该没问题。
但是,通常你不能在同一个带星号的元素上有两个指令,所以使用另一个 <ng-container>
可能是解决这个问题的方法:
<ng-container matColumnDef="role">
<mat-header-cell *matHeaderCellDef>Role</mat-header-cell>
<ng-container *ngIf="user.roles.length == 1">
<mat-cell *matCellDef="let user" >
Admin
</mat-cell>
</ng-container>
</ng-container>
关于angularjs - 在 mat-table 的 mat-cell 中实现 if else 条件 - Angular 5,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54116154/