微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

javascript-angular2 ng如果为true或false

有人可以在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);
    }
  }
}

模板中将没有局部变量.该模板只能直接访问组件类的顶级属性方法.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐