如何将* ngIf Else与变量一起使用

时间:2020-03-20 15:46:45

标签: angular angular-directive

例如我想用变量定义ng-template 像这样的东西

<ng-template
  #notExpandableIconRef
  let-variable="variable">
  <i class="far fa-edit mr-2 ml-1 pointer" (click)="onClick(variable)"></i>
</ng-template>

我有一个条件,当条件为假时,我需要显示notExpandableIconRef 像这样的东西

<i class="far fa-edit mr-2 ml-1 pointer" *ngIf="some condition; else notExpandableIconRef"></i>

但是问题是我找不到使用ngIfElse Directiove定义变量的方法,

当前,我已经通过这种方式解决了问题

<i class="far fa-edit mr-2 ml-1 pointer" *ngIf="some condition"></i>
<span *ngIf="!(some condition)">
   <ng-container *ngTemplateOutlet="notExpandableIconRef;context:{variable: someVariable}">. 
   </ng-container>
</span>

我的问题是:ngIfElse指令是否有一种简单的方法? 例如这样的东西

<i class="far fa-edit mr-2 ml-1 pointer" *ngIf="some condition; else notExpandableIconRef; SomehowDefineTheVariable"></i>

这是stackblitz示例:https://stackblitz.com/edit/angular-8-app-example-mta5z6?file=src/app/app.component.html

谢谢!

0 个答案:

没有答案