如何将 localStorage var 放入我的 ng-show 中? (Angular JS/Firebase)

How do I put a localStorage var inside my ng-show ? (Angular JS/Firebase)


情况:

当我加载页面但尚未登录时,一切正常,我在我的导航栏中只看到 LoginRegister 应该是这种情况。

但是当我登录并加载任何页面时,导航会在一种奇怪的状态下停留大约 0.5 到 1 秒,"Register", "Log in", "Profile" and "Log out" 全部同时出现。

然后导航会正常显示:仅显示 "Profile" and "Log out"

编辑: ng-cloack 修复了这个问题,但是当我登录时,配置文件和注销不会出现。


代码:

header.ejs

<html ng-app = "app">
        <div ng-controller="ctrlHead">
          <ul class="nav navbar-nav">
                <li ng-show="!authenticated"><a href="/users/register">JOIN</a></li>
                <li ng-show="!authenticated"><a href="/users/login">Login</a></li>
                <li ng-show="authenticated"><a href="/users/profile">ME</a></li>
                <li ng-show="authenticated" onclick="signOut();"><a>Logout</a></li>
          </ul>
       </div>

问题:

如何在我的 ng-show 中输入 localStorage 变量?

P.S.: header.ejs 是在所有页面上加载的 EJS 部分。


我尝试了什么:

这有效,但会在每个页面重新加载导航,导致闪烁。

app.factory("Auth", ["$firebaseAuth",
  function($firebaseAuth) {
    return $firebaseAuth();
  }
]);

app.controller("ctrlHead", ["$scope", "Auth", "$window",
  function($scope, Auth, $window) {
    $scope.auth = Auth;

    $scope.auth.$onAuthStateChanged(function(firebaseUser) {
        if (firebaseUser) {
            setAppCookie();
        }
        $scope.authenticated = firebaseUser;
    });
  }
]);

我尝试使用 localStorage:

app.factory("Auth", ["$firebaseAuth",
  function($firebaseAuth) {
    return $firebaseAuth();
  }
]);

app.controller("ctrlHead", ["$scope", "Auth", "$window",
  function($scope, Auth, $window) {
    $scope.authenticated = $window.localStorage.getItem("authenticated");
    $scope.auth = Auth;

    $scope.auth.$onAuthStateChanged(function(firebaseUser) {
        if (firebaseUser) {
            setAppCookie();
        }
        $window.localStorage.setItem("authenticated", firebaseUser);
        $scope.authenticated = $window.localStorage.getItem("authenticated");
    });
  }
]);

现在 ngCookies... 没有用:/无论身份验证状态如何,导航都停留在登录模式。

app.factory("Auth", ["$firebaseAuth",
  function($firebaseAuth) {
    return $firebaseAuth();
  }
]);

app.controller("ctrlHead", ["$scope", "Auth", "$cookies",
  function($scope, Auth, $cookies) {

    $scope.auth = Auth;

    $scope.auth.$onAuthStateChanged(function(firebaseUser) {

        if (firebaseUser) {
            setAppCookie();
            $cookies.put('authenticated', firebaseUser);
            $scope.authenticated = $cookies.get('authenticated');
        } else {
            $cookies.put('authenticated', firebaseUser);
            $scope.authenticated = $cookies.get('authenticated');
        }
    });

  }
]);

编辑: 我现在正在使用 angularfire,它工作正常,除了一件事,我需要将 authstate 存储在 localStorage 中。我一直在尝试使用 ng-storage 和 $window.localSorage 但它没有用...

您可以使用 angular ng-cloak 指令来防止 AngularJS html 模板以其原始(未编译)形式被浏览器短暂显示当您的应用程序正在加载时。

ngCloak documentation

<html ng-app="app" ng-cloak>
      <ul class="nav navbar-nav">
            <li ng-show="!authenticated"><a href="/users/register">JOIN</a></li>
            <li ng-show="!authenticated"><a href="/users/login">Login</a></li>
            <li ng-show="authenticated"><a href="/users/profile">ME</a></li>
            <li ng-show="authenticated" onclick="signOut();"><a>Logout</a></li>
      </ul>
</html>

您应该使用 $window 服务来访问在您的控制器范围之外创建的变量。最重要的是,您不应该两次创建(几乎)相同的控制器。

$window documentation

使用类似的东西(未测试):

firebase.auth().onAuthStateChanged(function(user) {
    console.log("CURRENT USER:" + firebase.auth().currentUser);
    app.controller('ctrl', function($rootScope, $window) {
        $scope.authenticated = $window.user != null;
        setAppCookie();
        // Compile the whole <body> with the angular module named "app"
        angular.bootstrap(document.body, ['app']);
    });
});

对于本地存储,你只需要使用常规的:

//Set item
localStorage.setItem("authenticated", JSON.stringify(firebaseUser));

//Get item
$scope.authenticated = JSON.parse(localStorage.getItem("authenticated"));

你已经声明了$rootScope,但是我没看到你在哪里注入..

记住所有依赖项都必须通过 $inject 依赖项注入..

    Var app = controller('mycontroller', ['$rootScope', function($rootScope) {
}]);