javascript 为什么 angular $resource 会向我的数据响应添加额外的对象($promise、$resolve...)?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22548161/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why does angular $resource add extra objects ($promise, $resolve...) to my data response?
提问by EKet
I return a resource with a URL
我返回一个带有 URL 的资源
$resource("http://foo.com/bar.json").get().
$promise.then(function(data){ $scope.result = data},
function(error){ $scope.msg = "error" } );
Resource returns
资源回报
["item1"...."item_n",.....,"$promise", "$resolved", "$get", "$save", "$query", "$remove", "$delete"]
Why do I get all those objects in my data set. I'm guessing $promise just returns all this and waits for the server response. But once I have the server response where can I just get my server data without the Promise jargon?
为什么我在我的数据集中得到所有这些对象。我猜 $promise 只是返回所有这些并等待服务器响应。但是,一旦我有了服务器响应,我可以在没有 Promise 行话的情况下从哪里获取我的服务器数据?
采纳答案by EKet
So in case someone else is stumbling here and didn't understand promises/angularjs here is what is going on. When you use .then()
or .get()
you get a promise and some helper functions all in the same object. This is awesome because then you don't worry about callbacks being defined and whether data is available because the promise object always has some properties. This object contains your raw data in another object within. So the promise object is nested, you just have to reference the data object within when the data is ready.
因此,如果其他人在这里磕磕绊绊并且不理解这里的 promises/angularjs,这就是正在发生的事情。当您使用.then()
or 时,.get()
您会在同一个对象中获得一个 promise 和一些辅助函数。这很棒,因为这样您就不必担心定义的回调以及数据是否可用,因为 Promise 对象总是有一些属性。这个对象包含你在另一个对象内的原始数据。因此 promise 对象是嵌套的,您只需在数据准备好时引用其中的数据对象。
Here's what I was doing
这是我在做什么
$resource("http://foo.com/bar.json").get().
$promise.then(function(data){ $scope.result = data},
//data is actually a promise object.
function(error){ $scope.msg = "error" } );
promise object
承诺对象
Note the data is actually under another object called "data". So in your success callback to get just the data you should do in this case: data.data
请注意,数据实际上位于另一个名为“数据”的对象下。所以在你的成功回调中,只获取你在这种情况下应该做的数据:data.data
回答by raygerrard
If you look at the angular source here:
如果您在此处查看角度源:
https://github.com/angular/angular.js/blob/master/src/ngResource/resource.js#L505
https://github.com/angular/angular.js/blob/master/src/ngResource/resource.js#L505
There is a toJSON method on the Resource prototype chain that will accomplish this for you.
Resource 原型链上有一个 toJSON 方法可以为您完成此操作。
For example:
例如:
$resource("http://foo.com/bar.json").get(function(res) {
$scope.result = res.toJSON();
});
回答by callmejuggernaut
You need to return wrapped result like {'result': { 'some_key': 'some_val' }} from your backend. Or just do like described above.
您需要从后端返回像 {'result': { 'some_key': 'some_val' }} 这样的包装结果。或者只是像上面描述的那样做。
Diary.getSharedWithMe(function(data) {
delete data.$promise;
delete data.$resolved;
_self.sharedDiariesWithMe = data;
}, function(error) {
console.log(error)
});
回答by Jason Goemaat
$resource
returns an object or array that will have your data when the call completes. All those functions are there to help you out and $resource
is mainly intended for CRUD operations. If you want the data, you have to wait for it to get returned so you might as well use the promise. If you want to strip all of those properties you can use angular.toJson
to convert it to json, but angular does that for you when posting it back to a resource or $http call so you shouldn't have to.
$resource
返回一个对象或数组,该对象或数组将在调用完成时包含您的数据。所有这些功能都是为了帮助您,$resource
主要用于 CRUD 操作。如果你想要数据,你必须等待它返回,所以你不妨使用承诺。如果您想去除所有这些属性,您可以angular.toJson
将其转换为 json,但 angular 在将其发布回资源或 $http 调用时会为您执行此操作,因此您不必这样做。
$scope.data = $resource("http://foo.com/bar.json").get();
// $scope.data does not have your data yet, it will be
// populated with your data when the AJAX call completes
...
// later in a call from a save button maybe you can just do
// this to post your changes back:
$scope.data.$save();
回答by musa
To automatically remove them from every request, you can add an interceptor:
要从每个请求中自动删除它们,您可以添加一个拦截器:
angular.module('app').config(config);
config.$inject = ['$httpProvider'];
function config($httpProvider) {
$httpProvider.interceptors.push(interceptor);
}
interceptor.$inject = [];
function interceptor() {
return {
request: (config) => {
if (config.data) {
delete config.data.$promise;
delete config.data.$resolved;
}
return config;
}
};
}