从列表中重新定义CRUD元素方法

Restangular CRUD element methods from list

本文关键字:定义 CRUD 元素 方法 列表 新定义      更新时间:2023-09-26

我正在使用一个AngularJS应用程序的Restangular,但是我遇到了一些CRUD操作的问题。

我正在尝试修补并从列表中删除项目。无论我怎么做,这些错误总是出现:

TypeError:项目。$ Scope. deleteitem

TypeError:项目。$ Scope. requesttitems . restangular .all. customget .then.$ Scope. patchtitlechange

这是我的代码块:
appModule
.controller('AppController', ['$scope', 'Restangular', '$rootScope', '$state', '$document',
    function ($scope, Restangular, $rootScope, $state, $document) {
        $scope.items = [];
        $scope.requestItems = function (search_input, page, status) {
            var request_params = {
                "search": search_input,
                "page": page,
                "filter_status": status
            };
            Restangular.all('/myUrl/3/').customGET("items", request_params).then(
                function (data) {
                    if (request_params.page == 1)
                        $scope.items = data.results;
                    else
                        $scope.items = $scope.items.concat(data.results);
                    $scope.metadata = {
                        "count": data.count,
                        "next": data.next,
                        "previous": data.previous
                    };
                    $scope.patchTitleChange = function (index) {
                        console.log($scope.items[index].name);
                        $scope.items[index].patch({name: $scope.items[index].name});
                    };
                    $scope.patchDescriptionChange = function (index) {
                        $scope.items[index].patch({description: $scope.items[index].description});
                    };
                }
            )
        };
        $scope.deleteItem = function (item) {
            item.remove().then(function () {
                var index = $scope.items.indexOf(item);
                if (index > -1)
                    $scope.items.splice(index, 1);
            })
        };

我看了其他与我的问题相关的问题,并尝试了他们的答案和解决方案,但似乎没有一个奏效。

分配给items数组的元素不是正则化元素。你在分配数据。结果,它是纯JSON对象。为了有补丁,删除,等等…它们必须通过Restangular传递。重新规范或直接接收get/getList请求:

//Collection
$scope.items = Restangular.restangularizeCollection(null, data.results, 'myUrl/3/items');
//Individual Items
angular.forEach(data.results, function (item) {
    $scope.items.push(Restangular.restangularizeElement(null, item, 'myUrl/3/items');
});

当重新正则化集合时,单个项也将被重新正则化,我只提到对结果集进行迭代,因为看起来您是在附加上面多个调用的结果。

参见:Restangular Methods