简体   繁体   English

使用从REST API返回的JSON数组以角度表?

[英]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 : 我有以下json结构,这些是我正在使用的模板附带的:

$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 : 该数组实际上可以满足所需的工作-但麻烦的是它是经过硬编码的,所以我有一个http get,它在字符串化时返回以下内容:

"[
    {
        "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 : 我用来从其余API获取json的代码如下:

    $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! 现在,我想用来自其余调用的信息而不是硬编码数组来设置$ scope.details……当我在http get中设置它时,我得到了$ scope.details未定义的错误! :( 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. 这可能是AJAX调用在后台运行并且您的其他代码正在执行的情况。

Check if Ajax is successfully getting the data: 检查Ajax是否成功获取数据:

$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 ? 您是使用Service还是Factory处理AJAX?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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