簡體   English   中英

AngularJS通過JSON API從用戶獲取相關數據

[英]AngularJS get related data from user through JSON API

我正在使用Ionic框架,但問題是關於AngularJS。 我在軌道上寫了紅寶石上的json api。 對於身份驗證,我選擇ng-token-auth + devise-token-auth

用戶json:

{
  "data": {
    "id": "1",
    "type": "users",
    "attributes": {
      "name": "User1",
      "email": "email@email.com",
      "current-sign-in-at": "..",
      "last-sign-in-at": "..",
      "created-at": ".."
    },
    "relationships": {
      "friends": {
        "data": [
              {
                  "id": "2",
                  "type": "users"
              }
         ]
       }
    }
  },
  "included": [
    {
      "id": "2",
      "type": "users",
      "attributes": {
        "name": "User2",
        "email": "user@user.com",
        "current-sign-in-at": "..",
        "last-sign-in-at": "..",
        "created-at": ".."
      },
      "relationships": {
        "friends": {
             "data": [ ]
         }
      }
    }
  ]
}

我的用戶序列化器:

class UserSerializer < ActiveModel::Serializer
  attributes :id, :name, :email,:current_sign_in_at,
              :last_sign_in_at, :created_at
  has_many :friends, through: :friendships
end

當前用戶對象作為響應:

{"success":true,"data":{"id":1,"provider":"email","uid":"0a56bb6b-dc72-4ef3-906e-1c17cb2fef46","name"
:"User1","nickname":null,"image":null,"email":"email@email.com"}}

有我的問題,我無法達到用戶關系信息。

我不知道為什么,但我的視圖中的對象名稱是user(不是current_user),並且在我的控制器中沒有訪問權限。 問題是我如何從該對象獲得一些額外信息以及如何為我的控制器提供當前用戶對象訪問。

我想通了,所以答案很簡單。 ng-token-auth提供$rootScope.user作為當前用戶,它可以在控制器中使用。 但我仍然不知道如何向用戶對象提供相關數據,幸運的是找到了解決方案以滿足我的需求。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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