gpt4 book ai didi

javascript - 使用 angular.copy() 复制从 ngResource 返回的数据时出错

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:16:15 25 4
gpt4 key购买 nike

我有以下代码:

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 数据时给我一个错误。

最佳答案

仔细看看angular.copy doc :

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成为相同的类型.

关于javascript - 使用 angular.copy() 复制从 ngResource 返回的数据时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17784052/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com