为什么此资源在使用$ delete方法后没有更新视图

Amy*_*yth 6 angularjs

在我的角度应用程序中,我有一个控制器如下:

function TemplateListControl($scope, TemplateService){
    $scope.templates = TemplateService.get(); // Get objects from resource

    // Delete Method
    $scope.deleteTemplate = function(id){
        $scope.templates.$delete({id: id});
    }
}
Run Code Online (Sandbox Code Playgroud)

在视图中,我有一个绑定templates模型的表.如下:

<table ng-model="templates">
    <thead>
        <tr>
            <th style="width:40%">Title</th>
            <th style="width:40%">controls</th>
        </tr>
    <thead>
    <tbody>
        <tr ng-repeat="template in templates">
            <td>
                <!-- Link to template details page -->
                <a href="#/template/[[template.id]]">
                    [[template.title]]
                </a>
            </td>
            <td>
                <!-- Link to template details page -->
                <a class="btn btn-primary btn-small"
                   href="#/template/[[template.id]]">Edit
                </a>
                <!-- Link to delete this template -->
                <a class="btn btn-primary btn-small"
                   ng-click="deleteTemplate(template.id)">Delete
                </a>
            </td>
        </tr>
    </tbody>
</table>
Run Code Online (Sandbox Code Playgroud)

现在当我点击上面模板中的删除链接时,它会调用该deleteTemplate方法并成功DELETE调用REST api.但是视图在刷新并$scope.templates = TemplateService.get();再次启动之前不会更新.我究竟做错了什么?

Aja*_*wal 1

您还必须更新客户端,因此修改源代码如下

 ng-click="deleteTemplate($index)">


$scope.delete = function ( idx ) {
  var templateid = $scope.templates[idx];

  API.Deletetemplate({ id: templateid.id }, function (success) {
    $scope.templates.splice(idx, 1);
  });
};
Run Code Online (Sandbox Code Playgroud)