谁能帮我解决这个 ngIf
条件,我试图在 key 不存在时隐藏注销,并在 localStorage
中存在时显示它。我对 ngIf
中的多个条件感到困惑,但检查是正确的。
此检查用作指令。
checkSession() {
var checkKey = localStorage.getItem('sessionKey');
if(checkKey == null){
var showLogout = false;
console.log('null key: ', checkKey);
} else {
showLogout = true;
//this check will only be available once the user is logged in
console.log('key exist: ' checkKey);
}
}
HTML
<a (click)="logout()" title="Logout" *ngIf="showLogout || (!showLogout && !showLogout)">
<i class="fa fa-sign-out fa-2x" aria-hidden="true" ></i>
</a>
最佳答案
看来你想要的是
export class MyComponent {
/// other stuff
showLogout:boolean = false;
checkSession() {
var checkKey = localStorage.getItem('sessionKey');
if(checkKey == null){
this.showLogout = false; // changed
console.log('null key: ', checkKey);
} else {
this.showLogout = true; // changed
//this check will only be available once the user is logged in
console.log('key exist: ' checkKey);
}
}
}
局部变量在模板中不可用。该模板只能直接访问组件类的顶级属性和方法。
关于javascript - angular2 ngIf 真或假条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38971359/