问题
SITUATION:
When I load a page and didn't log in yet, everything works fine and I only see Login
and Register
in my nav as should be the case.
But when I log in and I load any page, the nav will stay for about 0.5 to 1 seconds in a strange state where "Register", "Log in", "Profile" and "Log out"
all appear at the same time.
Then the nav appears as it should: showing only "Profile" and "Log out"
.
EDIT: ng-cloack
fixed this, but profile and log out don't appear when I log in.
CODE:
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>
QUESTION:
How do I input a localStorage var inside my ng-show ?
P.S.: header.ejs is an EJS partial that is loaded on all pages.
WHAT I TRIED:
This works but reloads the nav at each page, causing flickering.
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;
});
}
]);
My try using 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");
});
}
]);
And now with ngCookies
... Did not work :/ The nav is stuck in logged in mode regardless of auth state.
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');
}
});
}
]);
EDIT: I am now using angularfire and it works fine except for one thing, I need to store the authstate in localStorage. I have been trying with ng-storage and $window.localSorage but it didn't work...
回答1:
You can use the angular ng-cloak
directive in order to prevent the AngularJS html template from being briefly displayed by the browser in its raw (uncompiled) form while your application is loading.
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>
You should use the $window
service to access variable created outside of the scope of your controller. On top of that, you should not create the (almost) same controller twice.
$window documentation
Using something like that (not tested):
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']);
});
});
For local storage, you just need to use the regular one:
//Set item
localStorage.setItem("authenticated", JSON.stringify(firebaseUser));
//Get item
$scope.authenticated = JSON.parse(localStorage.getItem("authenticated"));
回答2:
You have declared the $rootScope, but I don't see where you injected..
Remember all dependencies have to be injected through the $inject dependency..
Var app = controller('mycontroller', ['$rootScope', function($rootScope) {
}]);
来源:https://stackoverflow.com/questions/41811343/how-do-i-put-a-localstorage-var-inside-my-ng-show-angular-js-firebase