0
在my.component.ts
,我有這部分我的模板爲什麼我的* ngIf檢測對象不是空的(Angular2)?
<li><a *ngIf="m_userO=={}" [routerLink]="['LoginPage']">Login</a></li>
<li><a *ngIf="m_userO!={}" (click)="logOut(m_userO)">{{m_userO.first_name}}<br>Logout</a></li>
和我出口
export class myComponent{
m_userO : userO = {};
}
在loginPage.component.ts
,我導入myComponent
,當他們按下登錄,這種情況
myComponent.m_userO = this.x_userO;
我已經確認x_userO
此時不是空的。
想法是,如果他們沒有登錄,它會說「登錄」,如果他們是,它會說「註銷」。問題是當我加載網站時,它已經顯示「註銷」,但我不知道爲什麼。謝謝。
你不能用'=='比較對象。你可以檢查它。定義'var a = {}'和'var b = {}'並檢查'(a == b)'或'(a === b)'none返回true。 Reginaldo建議您最好使用null或undefined,以確定是否未設置該值。 – stp18