Reputation: 10530
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?
Upvotes: 5
Views: 12444
Reputation: 10530
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.
Upvotes: 1
Reputation: 6720
IMHO, I think its fine to inject $rootScope
into a controller. I would recommend using emit/broadcast.
Upvotes: 10