如何在angular.bootstrap()之后在$ rootScope上设置值

时间:2012-11-02 15:36:35

标签: javascript angularjs

我正在使用angular.bootstrap()来初始化我的应用。我想在此之后为我的$ rootScope分配一些值。我正在尝试这个,但它不适合我:

var app = angular.module('myApp', []);
var modulesToLoad = ['myApp'];
angular.bootstrap(b, modulesToLoad);
app.run(function($rootScope) {
    $rootScope.isLoading = false;
}

2 个答案:

答案 0 :(得分:6)

引导函数返回$injector,因此您可以执行以下操作:

var app = angular.module('myApp', []);
var modulesToLoad = ['myApp'];
var injector = angular.bootstrap(b, modulesToLoad);
injector.invoke(function($rootScope) {
    $rootScope.isLoading = false;
});

答案 1 :(得分:0)

<body>
    {{globalText}}
    <div ng-controller="MyCtrl">
      {{localText}}
    </div> 

var myModule = angular.module('app2', []);
myModule.value('globalText', 'GLOBAL');
myModule.run(function($rootScope, globalText) {

  $rootScope.globalText = globalText;
});

myModule.controller('MyCtrl', function($scope) {
  $scope.localText = 'LOCAL';
});
相关问题