Looking to integrate a run
block into the AngularJS 1.3.8 code snippet below?
After several attempts, I noticed that when I include the run
block in my Angular app, the browser debugger in FireFox shows that the program briefly pauses at the beginning of the run
block before proceeding through the other sections like module
, config
, and controllers
. Eventually, the app fails to load altogether. However, when I remove the run
block, the Angular application functions correctly.
This code is adapted from a sample app, with the only modification being the addition of the run
block.
angular.module('hello', [/*'ngCookies',*/ 'ngRoute']).config(function($routeProvider, $httpProvider) {
$routeProvider.when('/', {
templateUrl: 'home.html',
controller: 'home',
controllerAs: 'controller'
}).otherwise('/');
$httpProvider.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest';
}).run(function ($rootScope, $cookies) {
var cookieValue = $cookies.get("myCookie");
if (cookieValue) {
$rootScope.myCookieVar = cookieValue;
}
}).controller('navigation',
function ($rootScope, $http, $location, $route) {
var self = this;
self.tab = function(route) {
return $route.current && route === $route.current.controller;
};
$http.get('user').then(function(response) {
if (response.data.name) {
$rootScope.authenticated = true;
} else {
$rootScope.authenticated = false;
}
}, function() {
$rootScope.authenticated = false;
});
self.credentials = {};
self.logout = function () {
$http.post('logout', {}).finally(function () {
$rootScope.authenticated = false;
$location.path("/");
});
}
}).controller('home', function ($http) {
var self = this;
$http.get('resource/').then(function(response) {
self.greeting = response.data;
})
});