使用angularjs从json获取值

时间:2016-11-14 12:18:41

标签: php angularjs json

我正在使用PHP创建一个离子登录应用程序,我有一个很好的登录功能,但我需要能够将某些值存储到本地存储以供以后使用。

以下是我的代码中的示例:

$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": "oyexs911@yahoo.com",
            "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/login.php?user=oyexs911@yahoo.com&pass=oyelaja",
            "headers": {
                "Accept": "application/json, text/plain, /"
            }
        },
        "statusText": "OK"
    }

我如何获得用户电子邮件,例如

2 个答案:

答案 0 :(得分:1)

您可以通过访问var email = $scope.profile.data.email

等data.email来获取电子邮件

在您收到电子邮件之前,您忘记了数据

{
  "data": [{
    "username": "ozombo",
    "email": "oyexs911@yahoo.com",
    ...
  }]
}

答案 1 :(得分:1)

您可以通过迭代$scope.profile.data.forEach(function(d) { console.log(d.email); }); 数组来访问用户电子邮件:

       1
    2     3
  4   5

希望有所帮助!