如何使用ng-repeat中的数据调用范围函数?

时间:2015-01-23 08:38:36

标签: javascript angularjs

如何在每次ng-repeat中自动调用控制器功能?我可以使用它吗?

角度观点:

<div ng-controller="Grid">
    <div class="row" data-ng-repeat="row in rows">
        <div class="col" data-ng-repeat="col in row.col">
            <div dnd-list="column.elements">
                <div ng-repeat="item in column.elements" ng-include="test.html"></div>
            </div>
        </div>
    </div>
</div>

<script type="text/ng-template" id="test.html'">
  <div style:"display: none;">{{item.grid_id = column.grid_id}}</div>
  <div style:"display: none;">{{item.position = $index}}</div>
  <-- {{addItemToArrayAndReorderGridEl(item, column, $index)}} -->
  <-- <div ng-model="addItemToArrayAndReorderGridEl(item, column, $index)" ></div> -->
</script>

Controller snippet:

$scope.items = [];
$scope.addItemToArrayAndReorderGridEl = function (item, column, position) {
  if ($scope.action_type === "edit") {
    $scope.items.push(item);
  }
  console.log($scope.items);
};

1 个答案:

答案 0 :(得分:4)

<div ng-controller="Grid">
  <div ng-repeat="a in b">
  <p ng-init="method($index)"></p>
  </div>
</div>

这样你就可以为每次迭代调用方法..