如何在 angular2 表达式中将 if-else 与字符串一起使用
How to use if-else with string in angular2 expression
我想通过 *ngFor
将 html 代码生成为 angular 2 表达式。
<ol class="breadcrumb">
<li><a href="#">Home</a></li>
<li><a href="#">Library</a></li>
<li class="active">Data</li>
</ol>
我使用这个代码,但它不是真的:
<ol class="breadcrumb">
<li *ngFor="let item of siteMap; let isLast = last" [class.active]="isLast">
{{isLast == true ? item : "<a href='#'>"+ item +"</a>"}}
</li>
</ol>
我该怎么办?
这对你有用。
<ol class="breadcrumb">
<li *ngFor="let item of siteMap; let isLast = last">
<a *ngIf="!isLast" href="#">{{item}}</a>
{{isLast ? item: ''}}
</li>
</ol>
这应该适合你。
<ol class="breadcrumb">
<li *ngFor="let item of siteMap; let isLast = last">
<a *ngIf="!isLast; else notLast" href="#">{{item}}</a>
<ng-template #notLast>{{isLast ? item: ''}}</ng-template> </li>
</ol>.
在同一个元素上使用 *ngIf 和 *ngFor 会引发错误。
我想通过 *ngFor
将 html 代码生成为 angular 2 表达式。
<ol class="breadcrumb">
<li><a href="#">Home</a></li>
<li><a href="#">Library</a></li>
<li class="active">Data</li>
</ol>
我使用这个代码,但它不是真的:
<ol class="breadcrumb">
<li *ngFor="let item of siteMap; let isLast = last" [class.active]="isLast">
{{isLast == true ? item : "<a href='#'>"+ item +"</a>"}}
</li>
</ol>
我该怎么办?
这对你有用。
<ol class="breadcrumb">
<li *ngFor="let item of siteMap; let isLast = last">
<a *ngIf="!isLast" href="#">{{item}}</a>
{{isLast ? item: ''}}
</li>
</ol>
这应该适合你。
<ol class="breadcrumb">
<li *ngFor="let item of siteMap; let isLast = last">
<a *ngIf="!isLast; else notLast" href="#">{{item}}</a>
<ng-template #notLast>{{isLast ? item: ''}}</ng-template> </li>
</ol>.
在同一个元素上使用 *ngIf 和 *ngFor 会引发错误。