5

I've defined some models in App.run below which I'm overriding within the controller someCtrl:

App.run(['$rootScope', function($rootScope) {
  $rootScope.attempt = 1;
});

function someCtrl($scope, $rootScope) {
  $rootScope.attempt = 2;

  $rootScope.checkAttempt = function () {
    return $rootScope.attempt > 1 ? true : false;    
  };
}

There is a button on the page out of someCtrl's scope:

<button class='btn' ng-disabled="checkAttempt()">Who's changing my value?</button>

FYI, I'm aware of creating a service or using emit-broadcaste mechanism to share data across controllers but I would like to know How authenticate is it to inject $rootScope into a controller?

4

3 に答える 3

10

IMHO, I think its fine to inject $rootScope into a controller. I would recommend using emit/broadcast.

于 2012-10-13T23:46:15.453 に答える
8

You don't need to inject $rootScope into a controller. When you inject $scope, you automatically have access to anything defined in $rootScope due to scope inheritance. See documentation. Scroll down to Scope Hierarchies section.

于 2013-05-16T16:40:35.143 に答える
1

Well, there is no harm in injecting a $rootScope into a controller, services or directives but you do try to figure out if its really necessary. The reason is that any method or property bound to $rootScope makes it global that will not be GC'ed unless manually cleaned up and it creates all those problems which global variables create.

The best way to share data across multiple controllers is to use a service.

于 2014-12-24T19:15:46.647 に答える