[英]*ngIf/else not working Angular4
I'm having trouble getting using *ngIf/else statements to work. 我无法使用* ngIf / else语句来工作。 Here is the setup: 这是设置:
<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>
The issue is my truthy statement is rendering (the button with View {{ daysBetween[i] | date:'EEEE' }}'s Plan
) on it, but my falsy value (the ng-template
) is not. 问题是我的truthy声明是渲染(带有View {{ daysBetween[i] | date:'EEEE' }}'s Plan
的按钮),但是我的假值( ng-template
)不是。 I've looked at a couple tutorials and I thought I am doing it right, but I've only been studying Angular4 for a few days now. 我看了几个教程,我认为我做得对,但我现在只研究Angular4几天了。 Please help! 请帮忙!
You could either use a ngSwitch
or, as you only have two cases, go directly for two ngIf
: 你可以使用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>
I think you should check your *ngIf condition once again. 我想你应该再次检查你的* ngIf情况。 I have tried same in my code it is working. 我在我的代码中尝试过相同的功能。
TestObj = [
{
category : 'Test'
}, {
category : 'Test1'
}
]
HTML Code : 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>
This is showing No category found
two times because of the condition. 这显示由于条件, No category found
两次No category found
。
Note : If you want to simplify condition you can do by checking subject condition in component and set flag true or false according to that. 注意:如果要简化条件,可以通过检查组件中的主题条件并根据该设置标志为true或false来完成。 And use that flag in *ngIf . 并在* ngIf中使用该标志。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.