[英]Global objects via services in Angular
進入我的應用程序后,我的GlobalApplicationController
初始化了整個應用程序中使用的一些服務(例如訂閱套接字等)。 擁有CurrentUser
對象將非常方便-但我不確定我得到的對象
@app.factory 'CurrentUser', ($http) ->
user = $http({
method: 'GET',
url: 'users/me'
}).then (data) ->
return data
返回的對象包含$$state
對象。 要獲取實際數據,我需要調用CurrentUser.$$state.value.data
。
我希望像CurrentUser.email
(或任何屬性)那樣調用我的服務,但是我不知道此返回對象是什么類型,或者為什么得到它。
編輯 :我發現,我得到的返回的對象是$http
對象(由於coffeescript)。 有沒有辦法返回數據( 不是承諾)?
從$ http一般用法then()文檔:
響應對象具有以下屬性:
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.
所以你想改變
return data
至
return data.data
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.