簡體   English   中英

通過Angular中的服務來全局對象

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM