angular - 我如何使用 "*ngIf else"?

标签 angular if-statement angular-template

我正在使用 Angular,我想在这个例子中使用 *ngIf else(从版本 4 开始可用):

<div *ngIf="isValid">
  content here ...
</div>

<div *ngIf="!isValid">
 other content here...
</div>

如何使用 ngIf else 实现相同的行为?

最佳答案

Angular 4 和 5:

使用 else :

<div *ngIf="isValid;else other_content">
    content here ...
</div>

<ng-template #other_content>other content here...</ng-template>

您还可以使用 then else :

<div *ngIf="isValid;then content else other_content">here is ignored</div>
<ng-template #content>content here...</ng-template>
<ng-template #other_content>other content here...</ng-template>

then独自一人:

<div *ngIf="isValid;then content"></div>
<ng-template #content>content here...</ng-template>

演示:

Plunker

详细信息:

<ng-template> : 是 Angular 自己对 <template> 的实现标签是 according to MDN :

The HTML <template> element is a mechanism for holding client-side content that is not to be rendered when a page is loaded but may subsequently be instantiated during runtime using JavaScript.

关于angular - 我如何使用 "*ngIf else"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43006550/

相关文章:

angular - 仅参数更改时,Angular 4 中的 routerLinkActive 不添加类

angular - 如何使用 Protractor 检查元素是否存在?

java - 一个类中的方法与另一类中的方法交互时出现问题

r - dplyr::rename() 如果满足列内容的条件

c++ - 你如何在 C++ 中比较多个字符串

html - ngStyle 从对象/模型接收颜色的问题

javascript - angular.js 指令 templateUrl 无法绑定(bind)范围

angular7 - 角 : how do I access the reference variable which is in a ng-template

ngFor 多上下文中的 Angular 6 ngTemplateOutlet

angular - 如何区分内部和外部 REST API 请求?