简体   繁体   中英

Using json array returned from rest api in angular for a table?

I have the following json structure which came with the template I am using :

$scope.details = [
    {
        name: 'jim'
        age: '21'
    },
    {
        name: 'mike';
        age: '60'
    }
];

The array actually works for what is needed - but the trouble is that it is hardcoded, so I have a http get which returns the following when stringified :

"[
    {
        "name": "Jim",
        "age" : "21"
    },
    {
        "name": "Mike",
        "age" : "60"
    }
]"

The code which I am using to get my json from the rest API is as follows :

    $http.get('http://localhost:8080/users/getAll').
        success(function(data) {
            console.log(JSON.stringify(data));
        });

Now, I want to set $scope.details with the info from the rest call instead of the hard coded arrays... and when I set it inside the http get, I get the error that $scope.details is undefined! :( Example :

    $http.get('http://localhost:8080/users/getAll').
        success(function(data) {
            $scope.details = data;
        });

All help is appreciated!

This may be a case that AJAX call is running in background and your other code is getting executed.

Check if Ajax is successfully getting the data:

$http.get('http://localhost:8080/users/getAll').
        success(function(data,status) {
            $scope.details = data;
            console.log($scope.details)
        });

Are you using Service or Factory for handling AJAX ?

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