2013-07-22 8 views
3

返回我有以下代碼:錯誤使用angular.copy時()來複制數據從ngResource

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) 

沒有人有我怎樣才能使數據RET的副本的想法urned。請注意,這些數據來自ngResource。此外,這似乎不同於我發現Stackoverflow相關的其他問題:TypeError:Object#沒有方法'推'的問題。有了這個問題,我得到的數據沒關係。它進入$ scope.grid.data,但在嘗試對數據進行angular.copy時會出現錯誤。

回答

9

angular.copydoc仔細看:

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

你從服務回報調用的結果可能是一個數組;你然而初始化$scope.grid.originalData作爲一個對象或數組以外的其他東西,所以你有這種類型的錯誤。

嘗試找出result是什麼類型,並在致電angular.copy之前使$scope.grid.originalData爲同一類型。