2016-11-14 81 views
1

我正在使用PHP在離子中創建登錄應用程序,我有一個登錄功能,它工作良好,但我需要能夠將某些值存儲到本地存儲以備後用。使用angularjs從json獲取值

這裏是我的代碼示例:

$scope.login = function() { 
    Login.login($scope.login).then(function (data) { 
     if (Object.keys(data.data).length === 1) { 
      var datao = angular.toJson(data); 
      $scope.profile = datao; 
      $scope.email = $scope.profile.email; 
      $scope.username = $scope.profile.username; 
      console.log(data); 
      console.log($scope.profile); 
       // $state.go('dashboard'); 
     } 
     .... 
    }); 
    .... 
}); 

$scope.profile顯示以下內容:

[Log] 
    { 
     "data": [{ 
      "id": "5", 
      "username": "ozombo", 
      "reputation": "ceo", 
      "activate": "8425", 
      "followercount": "3", 
      "praycount": "0", 
      "donetour": "0", 
      "fb_id": "0", 
      "fullname": "Adebambo Oyelaja", 
      "church": "RCCG ", 
      "photo": "1417451697.jpg", 
      "twtr_id": "0", 
      "screen_name": "", 
      "email": "[email protected]", 
      "bio": "Geek, Father, Husband", 
      "country": "Nigeria", 
      "state": "Lagos", 
      "followscount": "0", 
      "account": "", 
      "password": "5cf1bc1b9a2ada1ae9e29079aae1aefa", 
      "signup_date": "1413305984", 
      "signupdevice": "web", 
      "keycode": "5fc868f0767b0c164341a9e8e35edbe4", 
      "last_login": "1436519269", 
      "city": "Palmgroove", 
      "campaign": "", 
      "bday": "29-09-1988", 
      "gender": "Male", 
      "approved": "0", 
      "donewelcome": "0", 
      "phonenumber": "07017993683", 
      "secure": "private", 
      "churchid": "1", 
      "views": "68", 
      "marital": "Married" 
     }], 
     "status": 200, 
     "config": { 
      "method": "GET", 
      "transformRequest": [null], 
      "transformResponse": [null], 
      "url": "http://localhost:8888/opb/api/[email protected]&pass=oyelaja", 
      "headers": { 
       "Accept": "application/json, text/plain, /" 
      } 
     }, 
     "statusText": "OK" 
    } 

我將如何得到例如

回答

0

用戶郵件您可以通過獲得的電子郵件訪問data.email如var email = $scope.profile.data.email

您忘記了您有數據,然後才能收到電子郵件。

{ 
    "data": [{ 
    "username": "ozombo", 
    "email": "[email protected]", 
    ... 
    }] 
} 
+0

var email = $ scope.profile.data.email不工作...錯誤:undefined不是一個對象(評估'$ scope.profile.data.email') –

+0

如果$ scope.profile記錄了你在帖子中顯示你可以訪問像我說的 –

+0

是的我的日誌顯示我發佈的內容,但是當我這樣做時: $ scope.profile = datao; console.log($ scope.profile.data.email); 我得到錯誤:undefined不是一個對象(評估'$ scope.profile.data.email') –

0

您可以通過遍歷數組data可以訪問該用戶的郵箱:

$scope.profile.data.forEach(function(d) { 
    console.log(d.email); 
}); 

希望幫助!

+0

這樣做後,我得到錯誤:undefined不是一個對象(評估'$ scope.profile.data.forEach') –

+0

沒有任何方式,這可能是這種情況下,如果你提供的問題是準確的。你可以做'console.log($ scope.profile.data);'並顯示你的輸出? –

+0

undefined是我得到的 –