【问题标题】:Read json with JSONObject in angularjs在 angularjs 中使用 JSONObject 读取 json
【发布时间】:2015-02-12 16:36:14
【问题描述】:

按照本教程:http://www.w3schools.com/angular/angular_http.asp 我可以完美解析 json,但我遇到了问题。我有一个带有 json 的 php,其中有一个类似这样的 json:

{"item":[ 
{
"Name" : "Alfreds Futterkiste",
"City" : "Berlin",
"Country" : "Germany"
},
{
"Name" : "Berglunds snabbköp",
"City" : "Luleå",
"Country" : "Sweden"
}]}

如您所见,有一个 JSONObject (item).. 实际上要解析没有 JSONObject 的 json,我在控制器中执行此功能:

$http.get(JSONUrl)
        .success(function(response) {
            $scope.names = response;
        });

然后

<div data-ng-controller="MainController">
                <table class="uk-table uk-table-striped uk-table-hover" data-ng-if="!loading && !error">
                    <thead>
                    <th>Nome</th>
                    <th>Ver</th>
                    <th>Code</th>
                    </thead>
                    <tbody>
                    <tr data-ng-repeat="item in names">
                        <td>{{item.Name}}</td>
                        <td>{{item.City}}</td>
                        <td>{{item.Country}}</td>
                    </tr>
                    </tbody>
                </table>
            </div>

但在我的情况下,我没有显示任何内容,因为我将“项目”作为 JSONObject.. 任何解决方案?

【问题讨论】:

    标签: javascript php json angularjs


    【解决方案1】:

    您的问题是您没有在响应 JSON 对象中使用正确的属性

    使用

    <tr data-ng-repeat="item in names.item">
    

    而不是

    <tr data-ng-repeat="item in names">
    

    或

    $scope.names = response.item;
    

    【讨论】:

    • 效果太棒了!!没有关于那个权利的文件吗?谢谢
    • 当然,使用 ng-repeat 的文档应该涵盖它。需要明确的是,他所做的唯一更改是他引用了一个数组属性,而不是整个对象。您不会找到解释这一点的文档,那就是 javascript 101。如果您想在 name.item 上重复,请使用 name.item。
    • 哦,谢谢,我对 javascript 有点生疏了 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-09
    • 2016-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多