【问题标题】:RESTAngular PUT not saving entire payloadREST Angular PUT 不保存整个有效负载
【发布时间】:2014-01-17 16:48:06
【问题描述】:

我正在尝试使用 RESTAngular 发出 PUT 请求。我对 Angular 和 RESTAngular 都很陌生。

下面是代码 sn-p,它可以工作。

$scope.itemToUpdate = Restangular.all($scope.slug);
$scope.itemToUpdate.getList().then(function(items){
    var item = items.one($routeParams.id);
    item.name = $scope.singular.name;
    item.description = $scope.singular.description;
    item.put();
});

这不起作用。

    $scope.itemToUpdate = Restangular.all($scope.slug);
    $scope.itemToUpdate.getList().then(function(items){
        var item = items.one($routeParams.id);
        item = $scope.singular;
        item.put();
    });

不知道我做错了什么。

$scope.singular 最初获取它的数据如下。 Restangular.one('roles', $routeParams.id).getList().$object.

基本想法是从表单更新此模型,并在 slug 与 id 匹配时使用相关数据预填充表单。如果需要,我可以更改连接方式。因此,请随时提出最佳做法。

编辑 2

这个官方demo对解决问题很有帮助。 http://plnkr.co/edit/d6yDka?p=preview

【问题讨论】:

    标签: angularjs restangular


    【解决方案1】:

    当 Restangular 返回 resouce array\object 时,它会在对象上添加一些方法,例如 put,这些方法已被连接以在 put 调用服务器时更新对象。

    在第二种情况下,您分配item=$scope.singular$scope.singular 可能不是 Restangular 对象,因此不起作用。

    【讨论】:

    • 我更新了关于如何设置$scope.singular 的问题。这显然是数据类型不匹配。
    【解决方案2】:

    这个官方demo对解决问题很有帮助。 http://plnkr.co/edit/d6yDka?p=preview

    【讨论】:

    • 这个演示已经过时了。该库的作者已将 Restangular 的源代码 c&p'd 到演示中,而不是链接到当前的稳定源。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-07-10
    • 1970-01-01
    • 2018-09-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多