Restangular不会PUT完整对象

时间:2015-03-18 04:05:02

标签: javascript angularjs node.js express restangular

我对这一切都很陌生,所以如果我做了一些大规模的疏忽,就不要大声呐喊。

我尝试使用restangular更新表格行。然而,似乎并非所有对象数据都被设置为restapi。我用POSTMAN测试了restapi所以我知道它有效,我也创建了新对象。

使用google dev工具,PUT完成200 OK,但Requested Payload只有行的ID。实际上,如果我在带有数据的现有行上尝试这个,它会清除除ID之外的所有数据!

所以这就是我所拥有的:

HTML:

<div ng-controller="networksController">
<h1>{{title}}</h1>

<input type="text" ng-model="search" class="search-query" placeholder="Search">

<table class="table table-striped">
    <thead>
        <th>ID</th>
        <th>Title</th>
        <th>Reason</th>
        <th>Actions</th>
        <th>Requester</th>
        <th>Verified</th>
        <th></th>
    </thead>
    <tbody>
        <tr ng-repeat="change in changes | filter:search">
            <td>{{ change._id }}</td>
            <td>
                <div class="animate-show" ng-hide="editorEnabled">{{ change.title }}</div>
                <div class="animate-show" ng-show="editorEnabled">
                    <input class="animate-input" type="text" ng-model="change.title" />
                </div>
            </td>
            <td>
                <div class="animate-show" ng-hide="editorEnabled">{{ change.reason }}</div>
                <div class="animate-show" ng-show="editorEnabled">
                    <input class="animate-input" type="text" ng-model="change.reason" />
                </div>
            </td>
            <td>
                <div class="animate-show" ng-hide="editorEnabled">{{ change.actions }}</div>
                <div class="animate-show" ng-show="editorEnabled">
                    <input class="animate-input " type="text" ng-model="change.actions" />
                </div>
            </td>
            <td>
                <div class="animate-show" ng-hide="editorEnabled">{{ change.requester }}</div>
                <div class="animate-show" ng-show="editorEnabled">
                    <input class="animate-input" type="text" ng-model="change.requester" />
                </div>
            </td>
            <td>
                <div class="animate-show" ng-hide="editorEnabled">{{ change.verified }}</div>
                <div class="animate-show" ng-show="editorEnabled">
                    <input class="animate-input" type="text" ng-model="change.verified" />
                </div>
            </td>
            <td>
                <input type="button" value="Edit" ng-hide="editorEnabled" ng-click="editorEnabled=!editorEnabled" />
                <input type="button" value="Save" ng-show="editorEnabled" ng-click="editorEnabled=!editorEnabled; save(change)" />
                <button ng-click="destroy(change)">Delete</button>
            </td>
        </tr>
    </tbody>
</table>

主控制器:

var app = angular.module('richApp', ['ngResource', 'ngAnimate', 'restangular', 'xeditable'])
.config(function(RestangularProvider) {
            RestangularProvider.setBaseUrl('api/');
        });

页面控制器:

app.controller('networksController', ['$scope', '$resource', 'Restangular', function($scope, $resource, Restangular) {

    $scope.title = 'Network Control APP';

    var baseChanges = Restangular.all('changes');

    baseChanges.getList().then(function(changes) {
        $scope.allChanges = changes;
    });

    $scope.changes = Restangular.all('changes').getList().$object;

    $scope.destroy = function(change) {
        Restangular.one("changes", change._id).remove();
    };

    $scope.save = function(change) {
        Restangular.one("changes", change._id).put();
    };

}]);

2 个答案:

答案 0 :(得分:1)

好的,所以经过3天的高低狩猎后我终于找到了答案。 Mongo和restangular使用不同的ID键。 mongo使用_id而restangular使用id。

为了纠正这个问题,我需要添加以下内容

var app = angular.module('richApp', ['ngResource', 'ngAnimate', 'restangular', 'xeditable'])

.config(function(RestangularProvider) {
    RestangularProvider.setBaseUrl('api/');
    RestangularProvider.setRestangularFields({ //Added this
      id: "_id"                                //Added this
    });                                        //Added this
});

公平地说,我还修改了以下内容:

$scope.save = function(change) {
         Restangular.one("changes", change._id).get().then(function(data) {
            $scope.data = data;

        });

        var original = change;
        $scope.data = Restangular.copy(original);
        $scope.data.save();
    };

答案 1 :(得分:0)

我认为你应该使用

$scope.save = function(change) {
    change.put();
};