admin管理员组文章数量:1321823
Can someone help me with this ngIf
condition, I'm trying to hide the logout when key doesn't exist and show it when its exist in the localStorage
. I'm getting confused about multiple conditions in the ngIf
but the checks are correct.
This check is being used as a directive.
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>
Can someone help me with this ngIf
condition, I'm trying to hide the logout when key doesn't exist and show it when its exist in the localStorage
. I'm getting confused about multiple conditions in the ngIf
but the checks are correct.
This check is being used as a directive.
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>
Share
Improve this question
edited Aug 16, 2016 at 9:36
Günter Zöchbauer
658k233 gold badges2.1k silver badges1.6k bronze badges
asked Aug 16, 2016 at 9:34
nCorenCore
2,0977 gold badges31 silver badges59 bronze badges
1 Answer
Reset to default 4It seems what you want is
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);
}
}
}
A local variable won't be available in the template. The template can only access top-level properties and methods of your ponents class directly.
本文标签: javascriptangular2 ngIf true or false conditionStack Overflow
版权声明:本文标题:javascript - angular2 ngIf true or false condition - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742103739a2420923.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论