无法使用angularjs将json数据绑定到表

时间:2016-06-23 07:35:20

标签: javascript jquery angularjs json xml

This is My result in console log JSon data 您好我正在尝试将JSON对象数据绑定到table.my问题是我能够绑定整个json对象{{metric}}但是无法加载json的每个atttribute对象即{{metric.EmpId}}。 最后从我的观察结果中我发现转换后的json对象直接被赋予

$ scope.Employees =“员工”:[                               {“EmpId”:“4”,                                   “名字”:“克里斯”,                                   “性别男”,                                   “电话”: [                                     {                                         “_Type”:“Home”,                                         “__text”:“564-555-0122”                                     },                                     {                                         “_Type”:“工作”,                                         “__text”:“442-555-0154”                                     }                                   ]                                   “地址”: {                                       “街头”:“124 Kutbay”,                                       “城市”:“蒙塔拉”,                                       “州”:“CA”,                                       “Zip”:“94037”,                                       “国家”:“美国”                                   }                               }                             ]                         }

输出正如我预期的那样工作,但是当我指定直接结果时

即$ scope.Employees =响应;它可能无法解决问题

    <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></script>
    <script src="xml2json.js"></script>
     <script>
                var app = angular.module('httpApp', []);
                app.controller('httpController', function ($scope, $http) {
                    $http.get("File1.xml",
                            {
                                transformResponse: function (cnv) {
                                    var x2js = new X2JS();
                                    var aftCnv = x2js.xml_str2json(cnv);
                                    return aftCnv;
                                }
                            })
                    .success(function (response) {
                        console.log(response);
                        $scope.Employees = response;
                        console.log($scope.Employees);
                    });
                });
            </script>
 <div>
            <div ng-app="httpApp" ng-controller="httpController">
                <div ng-repeat="metric in Employees">

                    {{ metric}}
                    <br />
                    <br />
                    <table>
                        <tr ng-repeat="metric in Employees">
                            {{metric}}
                             <td ng-repeat="cell in metric">{{cell}}</td>
                            <td>{{cell.EmpId}}</td>
                            <td>{{metric.Name}}</td>
                        </tr>
                    </table>
                </div>
            </div>

1 个答案:

答案 0 :(得分:1)

假设您的json具有以下格式:

$scope.Employees = {
    "Employee": [
        {
            "EmpId": 1,
            "Name": "Sam"
        },
        {
            "EmpId": 2,
            "Name": "Lucy"
        }
    ]
};

您可能希望这样做:

<div ng-repeat="employeeList in Employees">
    <table>
        <tr ng-repeat="employee in employeeList">
             <td>{{employee.EmpId}}</td>
             <td>{{employee.Name}}</td>
        </tr>
    </table>
</div>