为什么角$资源添加额外的对象($承诺,$决心......)我的数据的反应? [英] Why does angular $resource add extra objects ($promise, $resolve...) to my data response?

查看:124
本文介绍了为什么角$资源添加额外的对象($承诺,$决心......)我的数据的反应?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我返回的资源与URL

I return a resource with a URL

    $resource("http://foo.com/bar.json").get().
         $promise.then(function(data){ $scope.result = data}, 
                  function(error){ $scope.msg = "error" } );

资源回报率

["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?

推荐答案

所以,如果别人在这里跌倒,不明白这里的诺言/ angularjs是怎么回事。当您使用。然后()获得()你得到一个承诺,一些辅助功能都在同一目的。这是真棒,因为你不担心被定义回调和数据是否可用,因为承诺的对象总是有一些属性。这个对象包含在另一个对象的原始数据。因此,承诺的对象是嵌套的,你只需要在数据准备中引用的数据对象。

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.

下面是我在做什么。

     $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" } );

承诺对象

请注意数据实际上是根据所谓的数据的另一个对象。因此,在您成功回调得到公正的数据,你应该在这种情况下,这样做:数据。数据

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

这篇关于为什么角$资源添加额外的对象($承诺,$决心......)我的数据的反应?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆