最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Error when using angular.copy() to copy data returned from ngResource - Stack Overflow

programmeradmin5浏览0评论

I have the following code:

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

and then the following function in 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;
        },

When I try to do an angular.copy I get a message saying:

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 pleteRequest (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) 

Does anyone have an idea of how I can make a copy of the data returned. Note that this data es back from ngResource. Also this seems different from the other problems that I find on Stackoverflow related to: TypeError: Object # has no method 'push' questions. With this problem I am getting the data back okay. It goes into $scope.grid.data but then gives me an error when trying to angular.copy the data.

I have the following code:

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

and then the following function in 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;
        },

When I try to do an angular.copy I get a message saying:

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 pleteRequest (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) 

Does anyone have an idea of how I can make a copy of the data returned. Note that this data es back from ngResource. Also this seems different from the other problems that I find on Stackoverflow related to: TypeError: Object # has no method 'push' questions. With this problem I am getting the data back okay. It goes into $scope.grid.data but then gives me an error when trying to angular.copy the data.

Share Improve this question edited Jul 22, 2013 at 9:36 asked Jul 22, 2013 at 9:29 user1943020user1943020
Add a ment  | 

1 Answer 1

Reset to default 9

Take a close look at angular.copy doc:

destination(optional) – {(Object|Array)=} – Destination into which the source is copied. If provided, must be of the same type as source.

The result you got back from service call is probably an array; you however initialized $scope.grid.originalData as an object or something else other than array, so you got this type error.

Try to figure out what type the result is, and make $scope.grid.originalData the same type before calling angular.copy.

发布评论

评论列表(0)

  1. 暂无评论