Friday, June 26, 2015

Difference between $scope and $rootscope

This article is written assuming you have the basic concepts of AngularJS !


Whenever you declare a controller in angular , you automatically create a $scope . So what does a $scope mean : “$scope is an object that refers to the application model. It is an execution context for expressions. $scopes are arranged in hierarchical structure which mimic the DOM structure of the application. $scope can watch expressions and propagate events.” documentation .

In simpler terms it means that it is a way of tying an object to the DOM . If you consider the MVC model in Angular , the scope acts as a model . It is a template which hosts all the functions and the related data . 

Okay now that we have gotten some insight into the $scope , let talk about $rootscope shall we ? $rootscope is the parent (more like a root parent ) of all the $scopes you create . It is the ultimate boss . It is (almost) like the Object class of Java .It is the top most $scope of your app and it contains ng-app directive . There is only one $rootscope for each of the Angular application .


No comments:

Post a Comment