如何从Angular JS控制器获取JSON数据

时间:2017-04-28 12:14:55

标签: javascript angularjs arrays json

我有用户登录..

942194
42143
23412

我不仅需要会员ID我希望会员中存在整个数据.... 我有以下控制器:

this.logincheck = function(log) {           
          var parameter ={
                    "mail" :log.mail,
                    "password" :log.passwords
                    }  
            alert(JSON.stringify(parameter));
            $http({
                url: '---------',
               -----------
            }).success(function(data,status) {
            if(data.status=="success")  {   
                var sessionid = data.member.member_id; //Member JSON is defined below
                $state.go('dashboardsnewsucc.dashboardefault', {userid: sessionid}); // How to pass entire object to user dashboard
            }
            else{
                alert("Invalid Username or Password try again. !!!");
            }
            }); 
    };

我从Web服务获得以下JSON数据。(用户登录后我获取此JSON数据)。

.state('dashboardsnew', {
            abstract: true,
            url: "/dashboardsnew",
            templateUrl: "views/common/content-empty.html",
        })

        .state('dashboardsnew.dashboardefault', {
            url: "/dashboardefault",
            templateUrl: "views/userdashboard.html",
            data: { pageTitle: 'Hive Dashboard',specialClass: 'loginscreen-gray-bg' },           
        })

以上JSON数据必须显示在登录主页中。使用普通的角度JS我应该使用它:

{
  "member": {
    "member_id": 51,
    "first_name": "Deepak",
    "last_name": "Verma",
    "phone": 6886438910,
    "password": "sushil",
    "role": [
      {
        "role_id": 2,
        "name": "Society Admin"
      }
    ],
    "associated": []
  },
  "society": {
    "society_id": 10,
    "society_name": "Green Velley"
  },
  "status": "success",
  "message": "member data details !"
}

在前端我想:

$http.get('http://192.168.1.7:8080/apartment//member/details/' + myParamtr).then(function(response) {
        $scope.myData = response.data.member;
}

2 个答案:

答案 0 :(得分:1)

如果您需要将数据从一个州保留到另一个州,在这种情况下,我建议使用角度服务:https://docs.angularjs.org/guide/services

您可以创建memberService,将成员数据设置为服务,重定向用户,并从下一个控制器中的服务中检索成员数据。

答案 1 :(得分:0)

url : your json file location with respect to your index file.
 $http.get(url).success(function(response) { 
       $scope.myData = response.member; // Your member data
       $scope.myData1 = response.member.first_name; // Your member firstName
});