user1765862 user1765862 - 4 months ago 23x
AngularJS Question

securing access to url and rootScope undefined

(function () {
"use strict";
var app = angular.module("myApp",

app.config(["$stateProvider", "$urlRouterProvider","$rootScope",
function ($stateProvider, $urlRouterProvider, $rootScope) {

.state("index", {
url: "/",
templateUrl: "app/index.html",
// Home page /* SECURED */
.state("home", {
url: "/home",
templateUrl: "app/home/home.html",
controller: "HomeController as vm",
data: {
requiresLogin: true
// Login
.state("login", {
url: "/login",
templateUrl: "app/login/login.html",
controller: "LoginController as vm"

$rootScope.$on('$stateChangeStart', function (e, to, userService) {
if ( && {
if (!userService.user.loggedIn()) {
//token not found/not valid


You didn't post what the problem is, but I assume it is that nothing works. If so here is obvious problem with your code: you are trying use $rootScope as module dependency to application module.

Correct module definition must be:

angular.module("myApp", [

I removed "$rootScope" from the list.

UPD. Closer look revealed another problem. You can't inject $rootScope into config block. Move it to the run:

  .run(['$rootScope', function($rootScope) {