如何在 ngIf 中使用相同的模板
How to use the same template in ngIf
我有很多条件来展示同一个模板。例如:
<template [ngIf]="item.url.indexOf('http') == -1">
<a class="ripple-effect" [routerLink]="[item.url]" *ngIf="isUserLoggedIn == true" >
<span class="media-body media-middle" *ngIf="isUserLoggedIn == true">{{item.name}}</span>
</a>
<p>Hello, World!</p>
</template>
<template [ngIf]="item.url.indexOf('http') == 0">
<a class="ripple-effect" [routerLink]="[item.url]" *ngIf="isUserLoggedIn == true" >
<span class="media-body media-middle" *ngIf="isUserLoggedIn == true">{{item.name}}</span>
</a>
<p>Hello, World!</p>
</template>
<template [ngIf]="item.url.indexOf('http') == 1">
<a class="ripple-effect" [routerLink]="[item.url]" *ngIf="isUserLoggedIn == true" >
<span class="media-body media-middle" *ngIf="isUserLoggedIn == true">{{item.name}}</span>
</a>
<p>Hello, World!</p>
</template>
<template [ngIf]="item.url.indexOf('http') == 2">
<a class="ripple-effect" [routerLink]="[item.url]" *ngIf="isUserLoggedIn == true" >
<span class="media-body media-middle" *ngIf="isUserLoggedIn == true">{{item.name}}</span>
</a>
<p>Hello, World!</p>
</template>
是否可以取这个html个元素:
<a class="ripple-effect" [routerLink]="[item.url]" *ngIf="isUserLoggedIn == true" >
<span class="media-body media-middle" *ngIf="isUserLoggedIn == true">
{{item.name}}
</span>
</a>
<p>Hello, World!</p>
并放在某处,然后在 *ngIf
中通过 name/reference 调用此 html 元素?例如:
<template [ngIf]="item.url.indexOf('http') == 2">
<!--reference to the repeatable code-->
</template>
实际上 ngIf 有一个 'cool' 属性 then
,您可以使用它:
<ng-container *ngIf="item.url.indexOf('http') === -1; then myTemplate">
</ng-container>
<ng-container *ngIf="item.url.indexOf('http') === 0; then myTemplate">
</ng-container>
<ng-container *ngIf="item.url.indexOf('http') === 1; then myTemplate">
</ng-container>
<ng-template #myTemplate>
<a class="ripple-effect" [routerLink]="[item.url]" *ngIf="isUserLoggedIn == true" >
<span class="media-body media-middle">{{item.name}}</span>
</a>
<p>Hello, World!</p>
</ng-template>
由于 <template>
已被弃用,请改用 <ng-template>
或 <ng-container>
。
您可以删除模板中的第二个 ngIf
,因为第一个就足够了。
是的,可以使用 NgIf DIRECTIVE
根据表达式的值有条件地包含模板。
ngIf 计算表达式,然后在表达式分别为真或假时渲染 then 或 else 模板。通常是:
- 除非绑定到 a,否则模板是 ngIf 的内联模板
不同的价值。
- else 模板是空白的,除非它被绑定。
下面是快照代码:
<div *ngIf="condition; then thenBlock else elseBlock"></div>
<ng-template #thenBlock></ng-template>
<ng-template #elseBlock></ng-template>
Angular4 ngIf会给你更多的细节。
希望这篇plunker对您有所帮助
我有很多条件来展示同一个模板。例如:
<template [ngIf]="item.url.indexOf('http') == -1">
<a class="ripple-effect" [routerLink]="[item.url]" *ngIf="isUserLoggedIn == true" >
<span class="media-body media-middle" *ngIf="isUserLoggedIn == true">{{item.name}}</span>
</a>
<p>Hello, World!</p>
</template>
<template [ngIf]="item.url.indexOf('http') == 0">
<a class="ripple-effect" [routerLink]="[item.url]" *ngIf="isUserLoggedIn == true" >
<span class="media-body media-middle" *ngIf="isUserLoggedIn == true">{{item.name}}</span>
</a>
<p>Hello, World!</p>
</template>
<template [ngIf]="item.url.indexOf('http') == 1">
<a class="ripple-effect" [routerLink]="[item.url]" *ngIf="isUserLoggedIn == true" >
<span class="media-body media-middle" *ngIf="isUserLoggedIn == true">{{item.name}}</span>
</a>
<p>Hello, World!</p>
</template>
<template [ngIf]="item.url.indexOf('http') == 2">
<a class="ripple-effect" [routerLink]="[item.url]" *ngIf="isUserLoggedIn == true" >
<span class="media-body media-middle" *ngIf="isUserLoggedIn == true">{{item.name}}</span>
</a>
<p>Hello, World!</p>
</template>
是否可以取这个html个元素:
<a class="ripple-effect" [routerLink]="[item.url]" *ngIf="isUserLoggedIn == true" >
<span class="media-body media-middle" *ngIf="isUserLoggedIn == true">
{{item.name}}
</span>
</a>
<p>Hello, World!</p>
并放在某处,然后在 *ngIf
中通过 name/reference 调用此 html 元素?例如:
<template [ngIf]="item.url.indexOf('http') == 2">
<!--reference to the repeatable code-->
</template>
实际上 ngIf 有一个 'cool' 属性 then
,您可以使用它:
<ng-container *ngIf="item.url.indexOf('http') === -1; then myTemplate">
</ng-container>
<ng-container *ngIf="item.url.indexOf('http') === 0; then myTemplate">
</ng-container>
<ng-container *ngIf="item.url.indexOf('http') === 1; then myTemplate">
</ng-container>
<ng-template #myTemplate>
<a class="ripple-effect" [routerLink]="[item.url]" *ngIf="isUserLoggedIn == true" >
<span class="media-body media-middle">{{item.name}}</span>
</a>
<p>Hello, World!</p>
</ng-template>
由于 <template>
已被弃用,请改用 <ng-template>
或 <ng-container>
。
您可以删除模板中的第二个 ngIf
,因为第一个就足够了。
是的,可以使用 NgIf DIRECTIVE
根据表达式的值有条件地包含模板。
ngIf 计算表达式,然后在表达式分别为真或假时渲染 then 或 else 模板。通常是:
- 除非绑定到 a,否则模板是 ngIf 的内联模板 不同的价值。
- else 模板是空白的,除非它被绑定。
下面是快照代码:
<div *ngIf="condition; then thenBlock else elseBlock"></div>
<ng-template #thenBlock></ng-template>
<ng-template #elseBlock></ng-template>
Angular4 ngIf会给你更多的细节。
希望这篇plunker对您有所帮助