我使用Angular,我想在本例中使用*ngIf else(从版本4开始提供):

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

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

我怎样才能用ngIf else实现相同的行为呢?

推荐答案

Angular 4 and 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>

Demo:

Plunker

Details:

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

HTML<template>元素是一种保存客户端的机制

Angular相关问答推荐

not getting circular input switch primeng组件

如何在自定义验证器中获取所有表单控件(字段组动态创建)

具有多重签名的Angular Mocking Service

为什么我的自定义.d.ts不起作用?LeaderLine不是构造函数

命令不起作用的初始菜单项

角material 17 -如何从Style.scss中的主题中获取原色

以17角表示的自定义元素

使用jsPDF生成带有图像的PDF时出现灰色条纹

天使17中的倒计时器

RxJs 中的空闲可观察对象在幕后做了什么?

iss 声明无效 Keycloak

Angular:try 在点击次数后禁用按钮

Angular2 测试 - 按 ID 获取元素

在 Angular2 中跟踪 Google Analytics 页面浏览量

Angular 2 单元测试 - @ViewChild 未定义

如何从 EventEmitter 函数返回值?

Angular 2:为什么在检索路由参数时使用 switchMap?

提交后在Angular 2中重置表单

Angular 5 和material - 如何从 SnackBar 组件更改背景 colored颜色

如何升级 Angular CLI 项目?