user1765862 user1765862 - 1 year ago 130
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

Answer Source

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) {
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download