简体   繁体   English

列表中的矩形CRUD元素方法

[英]Restangular CRUD element methods from list

I'm using Restangular with an AngularJS app, but am experiencing some problems with CRUD operations. 我将AngularJS应用程序与Restangular一起使用,但是CRUD操作遇到一些问题。

I'm trying to patch and remove items from a list. 我正在尝试打补丁并从列表中删除项目。 No matter what I do, these errors keep appearing: 无论我做什么,这些错误都会不断出现:

TypeError: item.remove is not a function at Scope.$scope.deleteItem TypeError:item.remove不是Scope。$ scope.deleteItem的函数

TypeError: item.patch is not a function at Scope.$scope.requestItems.Restangular.all.customGET.then.$scope.patchTitleChange TypeError:item.patch不是Scope。$ scope.requestItems.Restangular.all.customGET.then。$ scope.patchTitleChange的函数

This is my code block: 这是我的代码块:

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

I have looked at other problems related to mine and tried out their answers and solutions, but nothing seems to work. 我查看了与我的问题有关的其他问题,并尝试了它们的答案和解决方案,但似乎没有任何效果。

The elements assigned to your items array are not restangularized elements. 分配给您的items数组的元素不是重新矩形化的元素。 You are assigning data.results which are plain JSON objects. 您正在分配data.results,它们是普通的JSON对象。 In order to have patch, remove, etc... they must be passed through Restangular.restangularize or be directly received from get / getList requests: 为了进行补丁,删除等...它们必须通过Restangular.restangularize传递或直接从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');
});

When restangularizing a collection, the individual items will also be restangularized, I only mention iterating over the result set because it appears you are appending the results from multiple calls above. 在重新调整集合的范围时,各个项目也将重新调整范围,我只提到对结果集进行迭代,因为看来您要追加上面多个调用的结果。

See: Restangular Methods 另请: 矩形方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM