[英]How to remove $promise and $resolved from json object
I'm using $resource in angular to get json object and its structure is defined below 我在angular中使用$ resource来获取json对象,其结构定义如下
[
{
"id": 0,
"name": "Type1"
},
{
"id": 1,
"name": "Type 2"
}
]
after fetching the data .. console.log(jsonObject) gives me 获取数据后... console.log(jsonObject)给了我
[Resource, Resource, $promise: Object, $resolved: true]
How can I remove $promise & $resolved from the resulting object ? 如何从结果对象中删除$ promise和$ resolved? I tried angular.fromJson(json) but still I see that these objects still exist. 我尝试了angular.fromJson(json),但我仍然看到这些对象仍然存在。
I'm looking for the same answer, but at this moment I know only this ugly hack: 我正在寻找相同的答案,但此刻我只知道这个丑陋的黑客:
To write a func like this: 写一个像这样的函数:
function cleanResponse(resp) {
return JSON.parse(angular.toJson(resp));
}
and call it in every single query (ugly, yep): 并在每个查询中调用它(丑陋,是的):
function getSmt() {
Resource.get({}, function (data) {
$scope.some = cleanResponse(data);
}, function (responce) {
//handle error
})
}
I'll be happy if someone would teach me how to do it correctly 如果有人教我如何正确地做,我会很高兴
Example from my project 我的项目示例
Diary.getSharedWithMe(function(data) {
delete data.$promise;
delete data.$resolved;
_self.sharedDiariesWithMe = data;
}, function(error) {
console.log(error)
});
从这个答案 ,它看起来yourResource.toJSON()
随时可用。
Right, I've faced the same problem several times and always ended up using $http instead of $resource, however , today I decided to try and deal with this issue. 是的,我多次遇到同样的问题,总是使用$ http而不是$ resource, 但是 ,今天我决定尝试解决这个问题。 I hope somebody will find this useful one day as well. 我希望有人也会在某一天找到这个有用的东西。
So, after you receive your object with $promise inside of it, what you do is just use angular.copy(data)
, like so: 所以,在你收到带有$ promise的对象之后,你所做的只是使用angular.copy(data)
,如下所示:
someService.getSomething($scope.someId).$promise.then(function (data) {
if (data) {
$scope.dataWithoutPromise = angular.copy(data);
}
});
After that, you will see that your dataWithoutPromise
just contains the objects that you need, and $promise and $resolved are gone. 之后,您将看到dataWithoutPromise
只包含您需要的对象,$ promise和$ resolved消失了。
I'm facing the same issue. 我面临同样的问题。 In fact, you simply have to use the same version of angular and angular-resource and it will work like a charm. 实际上,您只需使用相同版本的角度和角度资源,它就像魅力一样。 Hope it helps ! 希望能帮助到你 !
Short answer : angular.fromJson(angular.toJson(resp)); 简答:angular.fromJson(angular.toJson(resp)); This will remove all "angular specific" functions etc and will return you a clean data object. 这将删除所有“角度特定”功能等,并将返回一个干净的数据对象。
Are you using isArray
or .query()
with your $resource
object? 您是否在您的$resource
对象中使用isArray
或.query()
?
From the $resource docs: isArray – {boolean=} – If true then the returned object for this action is an array.
来自$ resource docs: isArray – {boolean=} – If true then the returned object for this action is an array.
UPDATE: 更新:
If you are using $resource
correctly, some options you have are: 如果您正确使用$resource
,您可以使用以下选项:
1) Have the backend return the data contained within an object, eg. 1)让后端返回对象中包含的数据,例如。 { data: [] }
rather than just an array. { data: [] }
而不仅仅是一个数组。
2) If that is not possible, use the callback from the $resource.query()
, eg. 2)如果无法做到这一点,请使用$resource.query()
的回调,例如。
MyResource.query({ myParams: 'something'}, function(result) {
// Do something with the plain result
});
I did this quick reusable method for angular.toJson (Properties with leading $$ will be stripped): 我为angular.toJson做了这个快速可重用的方法(带有前导$$的属性将被剥离):
yourApp.factory('Service', ['$resource', function ($resource) {
return $resource('your/rest/api/path');
}]);
yourApp.factory('commonServices', function () {
return {
toJson : function(response){
return JSON.parse(angular.toJson(response));
//*or*
//return angular.toJson(response);
}
});
And then something like this in your controller: 然后在你的控制器中这样的事情:
yourApp.controller('Controller', ['$scope', 'Service','commonServices',
function ($scope, Service, commonServices) {
Service.get().then(function (data) {
$scope.myData = commonServices.toJson(data);
});
});
the more simple way is to add angular.toJson in the controller 更简单的方法是在控制器中添加angular.toJson
yourApp.controller('Controller', ['$scope', 'Service',
function ($scope, Service, commonServices) {
Service.get().then(function (data) {
$scope.myData = angular.toJson(data);
});
});
Please simply do this: 请简单地这样做:
jsonObject.$promise = undefined; jsonObject。$ promise = undefined; jsonObject.$resolved = undefined; jsonObject。$ resolved = undefined;
$promise and $resolved will not be removed but you will be able to do what you want. $ promise和$ resolved不会被删除,但你可以做你想要的。
Try to code something like this in your service: 尝试在您的服务中编写类似的代码:
yourApp.factory('Service', ['$resource', function ($resource) {
return $resource('your/rest/api/path');
}]);
And then something like this in your controller: 然后在你的控制器中这样的事情:
yourApp.controller('Controller', ['$scope', 'Service', function ($scope, Service) {
Service.get().then(function (data) {
$scope.myData = data;
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.