3

我有以下代码:

getContents: function ($scope, entityType, action, subjectId, contentTypeId, contentStatusId) {
                entityService.getContents(entityType, action, subjectId, contentTypeId, contentStatusId)
                .then(function (result) {
                    $scope.grid.data = result;
                    angular.copy($scope.grid.data, $scope.grid.originalData);
                    $scope.grid.newButtonEnabled = true;
                }, function (result) {
                    alert("Error: No data returned");
                    $scope.grid.newButtonEnabled = false;
                });
            },

然后entityService中的以下函数:

        getContents: function (entityType, action, subjectId, contentTypeId, contentStatusId) {
            var deferred = $q.defer();
            EntityResource.getEntities({ entityType: entityType, subjectId: subjectId, contentTypeId: contentTypeId, contentStatusId: contentStatusId },
               function (resp) {
                   deferred.resolve(resp);
               }
            );
            return deferred.promise;
        },

当我尝试执行 angular.copy 时,我收到一条消息:

TypeError: Object #<Object> has no method 'push'
    at Object.copy (http://127.0.0.1:81/Scripts/angular.js:600:21)
    at factory.getContents.entityService.getContents.then.$scope.grid.newButtonEnabled (http://127.0.0.1:81/Content/app/admin/services/grid-service.js:303:29)
    at deferred.promise.then.wrappedCallback (http://127.0.0.1:81/Scripts/angular.js:7303:59)
    at ref.then (http://127.0.0.1:81/Scripts/angular.js:7340:26)
    at Object.$get.Scope.$eval (http://127.0.0.1:81/Scripts/angular.js:8685:28)
    at Object.$get.Scope.$digest (http://127.0.0.1:81/Scripts/angular.js:8548:23)
    at Object.$get.Scope.$apply (http://127.0.0.1:81/Scripts/angular.js:8771:24)
    at done (http://127.0.0.1:81/Scripts/angular.js:10004:20)
    at completeRequest (http://127.0.0.1:81/Scripts/angular.js:10180:7)
    at XMLHttpRequest.xhr.onreadystatechange (http://127.0.0.1:81/Scripts/angular.js:10144:11) 

有谁知道我如何制作返回数据的副本。请注意,此数据来自 ngResource。此外,这似乎与我在 Stackoverflow 上发现的其他问题不同:TypeError: Object # has no method 'push' questions。有了这个问题,我可以恢复数据。它进入 $scope.grid.data 但在尝试 angular.copy 数据时给我一个错误。

4

1 回答 1

9

仔细查看angular.copy 文档

destination(optional) – {(Object|Array)=} – 将源复制到的目标。如果提供,必须与源的类型相同。

您从服务调用返回的结果可能是一个数组;但是,您将其初始化$scope.grid.originalData为对象或数组以外的其他内容,因此出现此类型错误。

尝试弄清楚它是什么类型result,并$scope.grid.originalData在调用之前制作相同的类型angular.copy

于 2013-07-22T13:26:07.150 回答