繁体   English   中英

angular2 ngIf真或假条件

[英]angular2 ngIf true or false condition

有人可以帮助我解决这个ngIf条件,我试图在密钥不存在时隐藏注销,并在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);
    }
  }
}

模板中将不提供局部变量。 该模板只能直接访问组件类的顶级属性和方法。

暂无
暂无

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

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