当ng-repeat嵌套在AngularDart中时访问外部$ index

时间:2014-02-13 19:52:28

标签: dart angular-dart

考虑以下嵌套ng-repeat指令:

  <tr ng-repeat="r in SomeExpr1">
    <td ng-repeat="c in SomeExpr2">
      <p>c index is {{$index}}, r index is {{???}}</p>
    </td>
  </tr>

如何访问外部$index的{​​{1}}(即内部ng-repeat范围ng-repeat隐藏的父作用域中的$index?< / p>

2 个答案:

答案 0 :(得分:17)

@Randal Schwartz在this post $parent中指出了这一点。

<div ng-controller='demo-ctrl'>
  <div ng-repeat="row in ctrl.matrix">
    <div ng-repeat="column in row">
      <span>outer: {{$parent.$index}} inner: {{$index}}</span>
    </div>

  </div>
</div>

答案 1 :(得分:5)

你需要来自Angular.js的ng-init。可悲的是,这还没有移植过,但是如果有效的话会看起来像这样:

<script>
  function Ctrl($scope) {
    $scope.list = [['a', 'b'], ['c', 'd']];
  }
</script>
<div ng-controller="Ctrl">
  <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>

(通过http://docs.angularjs.org/api/ng/directive/ngInit