What is $rootscope and how it is useful?

 Posted by Chvrsri on 3/28/2016 | Category: AngularJS Interview questions | Views: 750 | Points: 40
Answer:

$rootScope is just like a global variable which has the top most scope associated. Any app can have only one specific $rootScope which can be shared among all the components for that app.

Rest all $scopes will act as a children to the $rootScope. Since it is global any changes made to this will reflect in $scope of all other controllers. To add any thing in $rootScope we have to invoke app.run function. So it will ensure to run before to all other functionalities of the app.

app.run(function($rootScope) {

$rootScope.name = "AngularDotNetFunda";
});


The same can be used to print in this way

<body ng-app="myApp">

<h1>Hello {{ name }}!</h1>
</body>


Asked In: Many Interviews | Alert Moderator 

Comments or Responses

Login to post response