2016-06-16 5 views
0

제 질문은 isolatedScope, $ scope, $ rootScope를 포함하여 아래 코드에 얼마나 많은 범위 요소가 포함되는지입니다.angleApp에 얼마나 많은 Scope가 있습니까?

그래서 외부 ng-repeatlist.length 범위를 만들 것 ng-repeat 모든 행에 대한 새로운 scope를 생성 이후 $rootScope이 다음, ExampleController에서, 다음

컨트롤러 scope이있을 것있을 것

<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> 
+0

ng-repeat 및 ng-init은 어떻게됩니까? – harsh

답변

3

, ng-init는 것 새 범위를 만들지 않습니다. ng-repeat 내부가 ng-repeat 외부에 대해 한 번 실행되므로 각 목록 항목에 대해 innerList.length 범위가 생성됩니다.

이므로 전체 범위는 컨트롤러 + list.length * innerList.length에서 $rootScope + $scope이됩니다. 이 경우 1 + 1 + 2 * 2 = 6

(이 정보가 필요한 이유는 잘 모르겠지만, 여기있다)에서

그래서 총.

추신 : 어떤 directive이 새로운 (격리 된) scope을 생성하는지 알 수있는 설명서를 확인할 수 있습니다. 또한 innerList가 각 목록 항목에 대해 가변 길이를 갖는 경우 수식에는 직접 곱셈이 아닌 outerlist 길이에 대한 innerList 길이의 합계가 포함됩니다 (직접 곱셈이 아니라

즉 (1 * i1 + 1 * i2 +). .. outerlist.length times), 여기서 i1, i2는 내부 목록 길이입니다.

+0

ng-repeat는 각 행에 대해 격리 된 범위를 만들지 않습니다. 각 지시문에는 자체 범위가 있습니다. 2 ng-repeat = 2 scope – gyc

+0

문서의 @gyc : ngRepeat 지시어는 컬렉션의 항목 당 한 번 템플릿을 인스턴스화합니다. 각 템플릿 인스턴스는 자체 범위를 얻습니다. – gaurav5430

+0

감사합니다. @ gaurav5430을 간단한 방법으로 설명합니다 :) https://github.com/angular/angular.js/blob/master/src/ng/directive/ngRepeat.js#L3 – harsh

관련 문제