harsh
harsh

Reputation: 247

How many Scope's are there in an angularApp

My question is that how many scope elements would be in the below code including isolatedScope, $scope, $rootScope.

 <script>
          angular.module('initExample', [])
            .controller('ExampleController', ['$scope', function($scope) {
              $scope.list = [['a', 'b'], ['c', 'd']];
            }]);
        </script>
       <body ng-app="myApp">
        <div ng-controller="ExampleController">
          <div ng-repeat="innerList in list" ng-init="outerIndex = $index">
            <div ng-repeat="value in innerList" ng-init="innerIndex = $index">
               <span class="example-init">list[ {{outerIndex}} ][ {{innerIndex}} ] = {{value}};</span>
            </div>
          </div>
        </div>
 </body>

Upvotes: 0

Views: 52

Answers (1)

gaurav5430
gaurav5430

Reputation: 13902

There would be a $rootScope, then there would be Controller scope, from ExampleController,

then, since ng-repeat creates a new scope for every row, so outer ng-repeat would create list.length scopes, ng-init would not create a new scope, Now the internal ng-repeat runs once for each outer ng-repeat, so it would create innerList.length scopes for each list item.

so, total scopes would be : $rootScope + $scope from Controller + list.length * innerList.length.

So total in this case 1 + 1 + 2 * 2 = 6.

(I am not sure why this information is required, but here it is).

P.S: you can check the documentation to know which directives create a new(isolated) scope. Also note that, if the innerList has variable length for each list item, then the formula would include summation of innerList lengths over outerlist length and not a direct multiplication,

i.e, ( 1 * i1 + 1 * i2 + ... outerlist.length times ), where i1,i2 are inner list lengths

Upvotes: 3

Related Questions