简体   繁体   中英

How to get JSON data from Angular JS controller

I have an user login..

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. !!!");
            }
            }); 
    };

I not only want member ID I want entire data present in the member.... I have the following controller :

.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' },           
        })

I have the following JSON data from web service.( After user logged in I get this JSON data).

{
  "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 !"
}

Above JSON data has to be displayed in login home page. While using normal angular JS I supposed to use this :

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

In front end I supposed :

{{myData.user_name}} 

If you need to persist data from one state to another, in this case I recommend using an angular service: https://docs.angularjs.org/guide/services

You can create a memberService, set the member data to the service, redirect the user, and retrieve the member data from the service in your next controller.

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
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM