1

我有一张包含以下内容的表格:

 <tbody>
     <tr ng-repeat="result in ctrl.result track by $index">
       <td ng-bind="$index+1"></td>
       <td ng-bind="::result.title"></td>
       <td> <button type="button" class="btn" ng-click='ctrl.deleteItem(result)'>Delete</button></td>
     </tr>
 </tbody>  

在我的控制器中,我有:

 vm.deleteItem = function (result) {
        myService.deleteItem(result.id)
           .then(function (response) {
                vm.result.splice(result, 1);
            });
    };

如您所见,vm.result如果项目被成功删除,则 已更改。现在,该项目已在 db 中删除,因此我们有响应,然后该项目也已从 db 中删除vm.result。但该列表尚未在浏览器中更新。
如您所见,我使用controller as方法而不是 $scope.

4

2 回答 2

1

尝试以这种方式删除该项目:

vm.result.splice(vm.result.indexOf(result), 1);

第一个Splice参数应该是元素的索引而不是元素本身。

于 2016-06-08T05:47:51.947 回答
0
     var index = vm.result.findIndex(function(item) {
                if (item.id == test.id) {
                    return true;
                }
            });

            if (index !== -1) {
                // Removing the test from list;
                vm.result.splice(index, 1);
}

根据数组中的 id 检查 test.id 是要删除的元素的 id

于 2016-06-08T05:52:32.487 回答