Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

$scope and $rootscope variables' life term

In angularjs, I see a lot of usages for $scope and $rootscope. I like to understand the life term of variables attached to $scope and $rootscope. Say, I have a.js/a.html and e.js/e.html.

a.js has $scope attached with variables say $scope.b, $scope.c, $scope.d for use in a.html. If I load another page with e.js and e.html. Those $scope.b, $scope.c, $scope.d still have lives or they are removed. If I have $scope.b, $scope.c, $scope.d again in e.js, what could be issue? I just want to highlight the life term of those attached variables.

like image 882
batuman Avatar asked Apr 11 '26 01:04

batuman


2 Answers

  1. $rootScope - Usually configured in your main app.js file where you define your angular main module and main configuration. Even if not configured, it can always be injected.
  2. $scope - your current scope in the current controller (page).

  1. $rootScope variables live as long as you dont refresh your page. That means that they exist trough all transitions of your SPA.
  2. $scope variables live only on the current scope (again, as long as you dont refresh the page) and they will get disposed once you transition to a different controller/page.
like image 55
Tom Avatar answered Apr 12 '26 15:04

Tom


$rootScope lives as long as the whole page does

$scope instances are destroyed whenever the controller or directive using it are no longer active

No javascript is persistent between full page loads in browser

like image 22
charlietfl Avatar answered Apr 12 '26 14:04

charlietfl



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!