简体   繁体   English

angular2 ngIf真或假条件

[英]angular2 ngIf true or false condition

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 . 有人可以帮助我解决这个ngIf条件,我试图在密钥不存在时隐藏注销,并在localStorage存在时显示它。 I'm getting confused about multiple conditions in the ngIf but the checks are correct. 我对ngIf多个条件感到困惑,但检查是正确的。

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 HTML

 <a (click)="logout()" title="Logout" *ngIf="showLogout || (!showLogout && !showLogout)">
    <i class="fa fa-sign-out fa-2x" aria-hidden="true" ></i>
 </a>

It 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 components class directly. 该模板只能直接访问组件类的顶级属性和方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM