首页 > 解决方案 > 在 ng-repeat 之外传递对象

问题描述

所以我是AngularJs的新手。目前我正在尝试实现表过滤,然后当我单击删除按钮时,它应该删除正确的对象。

这就是我在过滤之前的做法:

$scope.rowIndex = -1;
$scope.selectRow = function(index) {
if (index == $scope.rowIndex)
  $scope.rowIndex = -1;
else
  $scope.rowIndex = index;
}
});

在我的 html 中:

ng-repeat="session in sessons " ng-class="{'bg-primary':rowIndex == $index }" ng-click="selectRow($index)"

现在在实现过滤之后我发现 $index 是错误的......所以我不得不找到另一种方法......我读了一些文章并且都说了同样的话......我可以将整个对象传递给函数......但是每个示例都在ng-repeat. 不幸的是......我不能这样做,因为我有一个用于模态的外部 div。

那么如何将当前选定的会话/表格行传递给模态中的函数?{{删除会话(会话)}}

<div id="deleteSessionModal" class="modal fade">
  <div class="modal-dialog">
    <div class="modal-content">
      <form>
        <div class="modal-header">
          <h4 class="modal-title">Delete Session</h4>
          <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
        </div>
        <div class="modal-body">
          <p>Are you sure you want to delete these Records?</p>
          <p class="text-warning">
            <small>This action cannot be undone.</small>
          </p>
        </div>
        <div class="modal-footer">
          <input type="button" class="btn btn-default" data-dismiss="modal" value="Cancel">
          <input type="submit" class="btn btn-danger" value="Delete" ng-click="deleteSession(session)">
        </div>
      </form>
    </div>
  </div>
</div>

这就是我的 html / 表格的样子

标签: javascriptangularjsangularjs-ng-repeat

解决方案


you can pass the session as the value to select row function like that:

ng-repeat="session in sessons " ng-class="{'bg-primary':rowIndex == $index }" ng-click="selectRow(session)"

and in the function selectRow, you can take the Id from the session and delete it from the sessions list.


推荐阅读