Is $rootScope the parent of the topmost $scope?

后端 未结 1 716
闹比i
闹比i 2021-02-05 11:39

I have a shared function which returns of the scope of the topmost element (document) in my AngularJS application.

function topScope() { 
  return angular.elemen         


        
1条回答
  •  醉酒成梦
    2021-02-05 12:40

    $rootScope is a parent scope of all scopes in a given AngularJS application. Since it is possible to bootstrap multiple AngularJS applications on one page (only manually, this can't be done using ng-app) it is also possible to have multiple $rootScope instances in one HTML documents.

    Each $rootScope is "attached" to either the element where ngApp was declared or the element passed into angular.bootstrap as described here.

    In short, the $rootScope is a root of all scopes for one AngularJS application but there is no "super-root" scope that would serve as a parent scope of all other scopes for a given HTML document.

    In your case using the $rootScope might be OK if you've got only one AngularJS application in the whole HTML document.

    0 讨论(0)
提交回复
热议问题