gpt4 book ai didi

javascript - 如何从 AngularJS 范围内的数组中删除一个项目?

转载 作者:IT老高 更新时间:2023-10-28 11:09:43 26 4
gpt4 key购买 nike

简单的待办事项列表,但在每个项目的列表页面上都有一个删除按钮:

enter image description here

相关模板HTML:

<tr ng-repeat="person in persons">
<td>{{person.name}} - # {{person.id}}</td>
<td>{{person.description}}</td>
<td nowrap=nowrap>
<a href="#!/edit"><i class="icon-edit"></i></a>
<button ng-click="delete(person)"><i class="icon-minus-sign"></i></button>
</td>
</tr>

相关 Controller 方法:

$scope.delete = function (person) {
API.DeletePerson({ id: person.id }, function (success) {
// I need some code here to pull the person from my scope.
});
};

我试过 $scope.persons.pull(person)$scope.persons.remove(person)

虽然数据库已成功删除,但我无法从范围中拉出此项目,并且我不想为客户端已经拥有的数据对服务器进行方法调用,我只想从范围中删除此人。

有什么想法吗?

最佳答案

您必须在 persons 数组中找到 person 的索引,然后使用数组的 splice方法:

$scope.persons.splice( $scope.persons.indexOf(person), 1 );

关于javascript - 如何从 AngularJS 范围内的数组中删除一个项目?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14250642/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com