I'm using $resource in angular to get json object and its structure is defined below
[
{
"id": 0,
"name": "Type1"
},
{
"id": 1,
"name": "Type 2"
}
]
after fetching the data .. console.log(jsonObject) gives me
[Resource, Resource, $promise: Object, $resolved: true]
How can I remove $promise & $resolved from the resulting object ? I tried angular.fromJson(json) but still I see that these objects still exist.
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)
});
From this answer, it looks that yourResource.toJSON()
is readily available.
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. 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:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With