微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

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

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

    // Delete Method
    $scope.deleteTemplate = function(id){
        $scope.templates.$delete({id: id});
    }
}

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

<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>

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

解决方法

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

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);
  });
};

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐