我正在使用 Angular 并且我想使用*ngIf else
(自版本 4 起可用)在此示例中:
<div *ngIf="isValid">
content here ...
</div>
<div *ngIf="!isValid">
other content here...
</div>
我怎样才能实现相同的行为ngIf else
?
角度 4 和 5:
Using 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>
Or then
alone:
<div *ngIf="isValid;then content"></div>
<ng-template #content>content here...</ng-template>
Demo:
Plunker https://plnkr.co/edit/XD5vLvmwTApcaHJ66Is1
Details:
<ng-template>
: 是 Angular 自己的实现<template>
标签是根据MDN https://developer.mozilla.org/en/docs/Web/HTML/Element/template:
超文本标记语言<template>
element 是一种保存客户端的机制
页面加载时不会呈现但可能会呈现的内容
随后在运行时使用 JavaScript 进行实例化。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)