[英]Global objects via services in Angular
After entering my application my GlobalApplicationController
initializes some services used through the whole application (like subscribing to sockets, etc.). 进入我的应用程序后,我的
GlobalApplicationController
初始化了整个应用程序中使用的一些服务(例如订阅套接字等)。 It would be pretty handy to have a CurrentUser
object - but I'm not sure about the object I get 拥有
CurrentUser
对象将非常方便-但我不确定我得到的对象
@app.factory 'CurrentUser', ($http) ->
user = $http({
method: 'GET',
url: 'users/me'
}).then (data) ->
return data
The returned object contains a $$state
object. 返回的对象包含
$$state
对象。 To get to the actual data I need to call CurrentUser.$$state.value.data
. 要获取实际数据,我需要调用
CurrentUser.$$state.value.data
。
I want my service to be called like CurrentUser.email
(or whatever attribute), but I have no idea what type this returned object is or why I'm getting it. 我希望像
CurrentUser.email
(或任何属性)那样调用我的服务,但是我不知道此返回对象是什么类型,或者为什么得到它。
Edit : I've found out that the returned object I get is the $http
object (due to coffeescript). 编辑 :我发现,我得到的返回的对象是
$http
对象(由于coffeescript)。 Is there a way to return the data ( not a promise)? 有没有办法返回数据( 不是承诺)?
From $http General Usage then() docs: 从$ http一般用法then()文档:
The response object has these properties:
响应对象具有以下属性:
data – {string|Object} – The response body transformed with the transform functions. status – {number} – HTTP status code of the response. headers – {function([headerName])} – Header getter function. config – {Object} – The configuration object that was used to generate the request. statusText – {string} – HTTP status text of the response.
So you would want to change 所以你想改变
return data
To 至
return data.data
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.