Šajā piemērā es izmantoju Angular un gribu izmantot *ngIf else
(pieejams kopš 4. versijas):
<div *ngIf="isValid">
content here ...
</div>
<div *ngIf="!isValid">
other content here...
</div>
Kā es varu panākt to pašu uzvedību ar ngIf else
?
Angular 4 un 5:
izmantojot else
:
<div *ngIf="isValid;else other_content">
content here ...
</div>
<ng-template #other_content>other content here...</ng-template>
var izmantot arī 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>
vai tikai then
:
<div *ngIf="isValid;then content"></div>
<ng-template #content>content here...</ng-template>
Demo :
[Plunker][1]
Detalizēta informācija:
<ng-template>
: ir paša Angular implementēta <template>
birka, kas saskaņā ar [MDN][2] :
HTML
<template>
elements ir mehānisms, kas paredzēts klienta puses saturu, kas nav jāattēlo, kad lapa tiek ielādēta, bet var pēc tam izpildīšanas laikā instancēt, izmantojot JavaScript.
"bindEmail" tas pārbaudīs, vai e-pasts ir vai nav pieejams. ja e-pasts ir pieejams, nekā Logout parādīs citādi Login parādīs
<li *ngIf="bindEmail;then logout else login"></li>
<ng-template #logout><li><a routerLink="/logout">Logout</a></li></ng-template>
<ng-template #login><li><a routerLink="/login">Login</a></li></ng-template>
Angular 4.0 versijā if..else
sintakse ir diezgan līdzīga nosacījuma operatoriem Java valodā.
Java izmanto "condition?stmnt1:stmnt2"
.
Angular 4.0 lietojat *ngIf="condition;then stmnt1 else stmnt2"
.