Reputation: 9184
Many tutorials in web have a little bit strange and huge logic of angularJS conditional routings...
i need something very simple:
just one component to check, if localStorage has authFlag as true, then something like:
app.config(function ($routeProvider) {
$routeProvider
.when('/', {
templateUrl: 'views/app.html',
controller: 'AppCtrl'
})
.when('/articles', {
templateUrl: 'views/articles.html',
controller: 'ArticlesCtrl',
conditions: { isAuth }
})
.otherwise({
redirectTo: '/'
});
});
how is it better to do? to check if authFlag is in localStorage, if not, then root to '/' ?
Upvotes: 1
Views: 1634
Reputation: 7475
you need to add resolve, then in the resolve funciton, add check for the local storage, and route to "/" if not:
in the resolve funciton add the following code
resolve:{
data: function(){
var result = window.localStorage.getItem("authFlag");
if (result)
{
// do whatever you want to do
}
else
{
$location.path("/");
}
}
},
Upvotes: 4