Reputation: 1269
I want the users not to go to certain pages at least they've logged in before. I'm currently using this:
app.run(function ($rootScope, $route, $location)
{
var restrictedPages =
[
'/Survey/', '/Survey/Detalle', '/Survey/Accepted', '/Account/Profile',
'/Auction/Detail', '/Survey/Accepted'
];
$rootScope.$on('$locationChangeStart', function (ev, next, current)
{
var nextPath = $location.path();
var nextRoute = $route.routes[nextPath];
if(restrictedPages.indexOf(nextPath) !== -1)
{
$location.path('/home');
}
});
});
My problem here is that I want to inject inside of this piece of code my AccountService. How can I achieve this? Because the loading-order is the following
app.js (the code presented is inside here)
homeService.js
I truly believe this is not the right way to go but it seems so simple and the only thing I'm missing is the account service injection.
Upvotes: 0
Views: 1124
Reputation: 147
Consider this module, which includes a accountService that uses implicit DI:
angular.module('myApp', [])
.factory('accountService', function($rootScope) {
// $rootScope is implicitly injected
})
.run(['$rootScope', '$route', '$location', 'accountService', function ($rootScope, $route, $location, accountService)
{
var restrictedPages =
[
'/Survey/', '/Survey/Detalle', '/Survey/Accepted', '/Account/Profile',
'/Auction/Detail', '/Survey/Accepted'
];
$rootScope.$on('$locationChangeStart', function (ev, next, current)
{
var nextPath = $location.path();
var nextRoute = $route.routes[nextPath];
if(restrictedPages.indexOf(nextPath) !== -1 && !accountService.isLoggedIn())
{
$location.path('/home');
}
});
}]);
if you want more documentation: https://docs.angularjs.org/guide/di
Upvotes: 2