Angular2 * ngIf无法正常工作

时间:2017-04-03 21:47:57

标签: javascript angular

HTML

<li class="nav-item">
  <a class="nav-link" routerLink="/register" *ngIf="isLoggedIn">Register</a>
</li>

JS:

var auth = this.af.auth.subscribe( (user) => {
      if (user) {
        // User signed in!
        var uid = user;
        var isLoggedIn = true;
        console.log(uid)
        console.log(isLoggedIn);
      } else {
        // User logged out
        console.log("no user")
        var isLoggedIn = false;
        console.log(isLoggedIn);
      }
    });

控制台正确记录了true和false值,因此var isLoggedIn正在正确更改,但* ngIf无论如何都会保持隐藏状态。

1 个答案:

答案 0 :(得分:5)

您似乎正在isLoggedInif块中重新声明else。相反,您应该分别设置为this.isLoggedin = truethis.isLoggedin = false