Setting $ rootScope to defaults doesn't work

I would like to have several global variables that are associated with the user interface (i.e. the open state of the menu). I decided to put them in $rootScopeso that they are always available.

This is the code I wrote:

(function (angular) {

    angular
        .module("App", [])
        .run(["$rootScope", function ($rootScope) {
            angular.extend($rootScope, {
                ui: {
                    menu: false,
                    ...
                }
            });
        }]);

})(angular);

I deliberately used angular.extendto make sure that successive manipulations with the same object were saved. And I think this is also cleaner and safer than adding multiple properties to any object one by one.

Problem

The top code does nothing. When I launch my application and check the root area by calling:

$("body").scope().$root.ui

undefined, - ui ng-click . ui ... , run(), angular , ng-click, .

?

+4
1

-

. , , , . , .

:

app.js
routing.js
service.something.js
...
filter.something.js
...

( ti ). . .

, . app.js , .

, , . Lasy- Google.

,

instanceInjector.loadNewModules = function (mods) {
    forEach(loadModules(mods), function(fn) { instanceInjector.invoke(fn || noop); });
};

, :

$injector.loadNewModules(["Module1", "Module2", ...]);

( ), ​​ Module, requires(), :

angular.module("MainModule").requires(["AdditionalModule", "OtherModule", ...]);

, .

0

All Articles