简体   繁体   中英

How to call scope function with data from ng-repeat?

How to call controller function automatically in each ng-repeat? Can I use for it directive?

Angular view:

<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);
};
<div ng-controller="Grid">
  <div ng-repeat="a in b">
  <p ng-init="method($index)"></p>
  </div>
</div>

This way you can call method for each iteration..

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM