0

I am reading through some of the docs for optimizely and saw an interesting value that they add to scope. I am not sure that I fully understand it.

Here is the function in their docs

 function(activate, options) {
   var scope = window.angular.element('body').scope();
   scope.$on('$locationChangeSuccess', function(event, next, current) {
     if (next.indexOf('/productPage') != -1) {
       if (!options.isActive) {
         activate();
       }
     }
   });
 }

I do not completely follow var scope Is this similar to a * $rootscope * ?

I know you can get the value of an associated scope by doing this

angular.element($0).scope()

I see window and want to assume this is beyond a local scope.

Winnemucca
  • 3,309
  • 11
  • 37
  • 66

1 Answers1

1

It is equal to current application's $rootScope as long as it was bootstrapped with body element, and there are no directives on body that have their own scopes. As long as the app was bootstrapped with body or html, it will work for current piece of code because $locationChangeSuccess is broadcasted to all scopes.

For the case when debugging data is enabled the failproof methods are

var root = angular.element(document.querySelector('.ng-scope')).scope().$root;

and

var root = angular.element(document.querySelector('.ng-scope')).injector().get('$rootScope');
Estus Flask
  • 206,104
  • 70
  • 425
  • 565