[英]*ngIf/else not working Angular4
我無法使用* ngIf / else語句來工作。 這是設置:
<div *ngFor="let sub of day">
<p>{{ sub.subject }}</p>
<button type="button" *ngIf="sub.subject; else showElse" routerLink="/day" (click)="viewIndividual(day)" class="btn btn-{{colors[i]}}">View {{ daysBetween[i] | date:'EEEE' }}'s Plan</button>
<ng-template #showElse>
<a type="button" class="btn btn-primary">Add a Lesson</a>
</ng-template>
</div>
問題是我的truthy聲明是渲染(帶有View {{ daysBetween[i] | date:'EEEE' }}'s Plan
的按鈕),但是我的假值( ng-template
)不是。 我看了幾個教程,我認為我做得對,但我現在只研究Angular4幾天了。 請幫忙!
你可以使用ngSwitch
或者,因為你只有兩個案例,直接去兩個ngIf
:
<div *ngFor="let sub of day">
<p>{{ sub.subject }}</p>
<button *ngIf="sub.subject" type="button" routerLink="/day" (click)="viewIndividual(day)" class="btn btn-{{colors[i]}}">View {{ daysBetween[i] | date:'EEEE' }}'s Plan</button>
<a *ngIf="!sub.subject" type="button" class="btn btn-primary">Add a Lesson</a>
</div>
我想你應該再次檢查你的* ngIf情況。 我在我的代碼中嘗試過相同的功能。
TestObj = [
{
category : 'Test'
}, {
category : 'Test1'
}
]
HTML代碼:
<div *ngFor="let key of TestObj">
<p *ngIf="key.subcategory; else newText">** {{key.category}}</p>
<ng-template #newText>No category found</ng-template>
</div>
這顯示由於條件, No category found
兩次No category found
。
注意:如果要簡化條件,可以通過檢查組件中的主題條件並根據該設置標志為true或false來完成。 並在* ngIf中使用該標志。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.