\$scope vs \$rootscope

amaneet
Kilo Contributor

May i know what is difference between scope and rootscope.

How do we decide which one to use?

1 ACCEPTED SOLUTION

Harsh Vardhan
Giga Patron

$scope: It is like a local variable and it can be accessible only within the component eg. controller,service


when we want any variable to be used within the class or component or want without having a reference to the containing class anyone can access that element then we should use $scope as it would not allow this variable to be accessible from outside.


$rootscope: It is like the global variable and it can be accessible from everywhere.


a common property that can be shared with anyone no restrictions on accessibility. a shared resource which can be accessed from anywhere.



for more information refer the above link that i had shared


View solution in original post

3 REPLIES 3

Harsh Vardhan
Giga Patron

Harsh Vardhan
Giga Patron

$scope: It is like a local variable and it can be accessible only within the component eg. controller,service


when we want any variable to be used within the class or component or want without having a reference to the containing class anyone can access that element then we should use $scope as it would not allow this variable to be accessible from outside.


$rootscope: It is like the global variable and it can be accessible from everywhere.


a common property that can be shared with anyone no restrictions on accessibility. a shared resource which can be accessed from anywhere.



for more information refer the above link that i had shared


jyoti29208
Giga Expert

HI Amaneet,



In simpler words:


$scope: We can get ng-model properties in particular controller from the particular page by using this


$rootscope: We can get all ng-model properties in any controller from any page by using this.



consider one scenario . you have one main page and in that page you have different templates. every templates has their own controller. There is one ID which you need in every controller. Fetch that ID from service in once controller n save it in $rootscope. so that it will accessible for every controller.



Please provide your feedback appropriately (Like, Helpful, Endorse AND/OR Correct) to help community.


Thank you!



Regards,


Jyoti