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?
Root Scope All applications have a $rootScope which is the scope created on the HTML element that contains the ng-app directive. The rootScope is available in the entire application. If a variable has the same name in both the current scope and in the rootScope, the application uses the one in the current scope.
An app can have only one $rootScope which will be shared among all the components of an app.
Q 18 - Which of the following is true about ng-controller directive? A - ng-controller directive tells AngularJS what controller to use with this view. B - AngularJS application mainly relies on controllers to control the flow of data in the application.
The ng-controller="myCtrl" attribute is an AngularJS directive. It defines a controller. The myCtrl function is a JavaScript function. AngularJS will invoke the controller with a $scope object.
IMHO, I think its fine to inject $rootScope
into a controller. I would recommend using emit/broadcast.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With