I would like to have few global variables that are UI related (i.e. open menu state). I decided to put these in $rootScope
so they're always accessible.
This is the code I've written:
(function (angular) {
angular
.module("App", [])
.run(["$rootScope", function ($rootScope) {
angular.extend($rootScope, {
ui: {
menu: false,
...
}
});
}]);
})(angular);
I've deliberately used angular.extend
, to make sure that consecutive manipulations to the same object are retained. And I think it's also more clean and safe than adding several properties to any object one by one.
Problem
Upper code doesn't do anything though. When I run my application and check root scope by calling:
$("body").scope().$root.ui
I get undefined
until some property within ui
gets manipulated by ng-click
directives on my application. Only then my ui
gets a reference... But it's still not the one I've defined in the run()
function but rather angular generated object property that ng-click
directive generated as per expression.
What am I doing wrong?