javascript - 在 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">×</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>
解决方案
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.
推荐阅读
- python - 全局列表在递归调用期间追加时突然更新
- php - 如何检查条件是否为真,然后在竞争条件下插入一些东西
- docker - 在 Dockerfile 中下载一个 git 存储库
- javascript - Magento 2 - 如何将 javascript 函数添加到我的 html 自定义表单
- android - 链接 RxJava 可观察对象以在 Room 数据库中更新/创建条目
- python - mypy 错误:“copyfile”的类型变量“_AnyPath”的值不能是“Union [str, Path]”
- selenium - Appium 服务器与 Selenium 服务器有何不同
- javascript - 为什么 clearInterval 不清除我的间隔?
- sql - 简单更新 (SQL) 与批量更新 (PL/SQL)
- java - 使用 Autowired 类测试 void 方法