迭代angularjs中对象的属性列表

时间:2015-10-20 03:18:38

标签: javascript arrays angularjs angularjs-scope angularjs-ng-repeat

我有这个对象的数组:

"{
    "Drivers":[
        {
            "Id":"41ba341a-e50f-4878-b55e-6c96ef5027b0",
            "FirstName":"John",
            "LastName":"David",
            "MiddleName":"Smity",
            "Party":{
                "Id":"12rtb67-fa45-4e77-998c-123fgyh764",
                "Name":"The Smith Family",
                "CustomerId":"01d84752-ad12-4dfa-af63-65d12052a161"
            }
        },
        {
            "Id":"f37ff0c7-294d-43d1-a790-11af1f6f3e91",
            "FirstName":"John",
            "LastName":"Smith",
            "MiddleName":"David",
            "Party":{
                "Id":"f2a82558-fa45-4e77-998c-29160f30f9f6",
                "Name":"The Smith Family",
                "CustomerId":"01d84752-ad12-4dfa-af63-65d12052a161"
            }
        }
    ]
}"

我想遍历Drivers对象并在angularjs foreach中显示值。

这就是我正在尝试的:

<table data-ng-controller="DriverController">
  <thead>
    <tr>
      <th>Name</th>
    </tr>
  </thead>
  <tbody data-ng-repeat="driver in Drivers.Drivers">
    <tr>
      <td>{{driver.FirstName}}</td>
    </tr>
  </tbody>
</table>

它没有帮助。

我也尝试过:

<table data-ng-controller="DriverController">
  <thead>
    <tr>
      <th>Name</th>
    </tr>
  </thead>
  <tbody data-ng-repeat="driver in Drivers">
    <tr>
      <td>{{driver.FirstName}}</td>
    </tr>
  </tbody>
</table>

但是我得到了相同的结果,屏幕上没有显示数据。

我知道如何解决这个问题并迭代数组的属性并在表格中显示结果,因为我在这个例子中尝试了吗?

由于

2 个答案:

答案 0 :(得分:0)

您的JSON是示例中的字符串。如果这是您从服务器接收它的方式,则需要解析它。除此之外,您的代码适用于此plunker:http://plnkr.co/edit/x2IJRCUUTqpYnombadzn?p=preview

<table data-ng-controller="DriverController">
                    <thead>
                        <tr>
                            <th>Name</th>
                        </tr>
                    </thead>
                    <tbody data-ng-repeat="driver in drivers.Drivers">
                        <tr>
                            <td>{{driver.FirstName}}</td>
                        </tr>
                    </tbody>
                </table>


$scope.drivers = {
    "Drivers":[
        {
            "Id":"41ba341a-e50f-4878-b55e-6c96ef5027b0",
            "FirstName":"John",
            "LastName":"David",
            "MiddleName":"Smity",
            "Party":{
                "Id":"12rtb67-fa45-4e77-998c-123fgyh764",
                "Name":"The Smith Family",
                "CustomerId":"01d84752-ad12-4dfa-af63-65d12052a161"
            }
        },
        {
            "Id":"f37ff0c7-294d-43d1-a790-11af1f6f3e91",
            "FirstName":"John",
            "LastName":"Smith",
            "MiddleName":"David",
            "Party":{
                "Id":"f2a82558-fa45-4e77-998c-29160f30f9f6",
                "Name":"The Smith Family",
                "CustomerId":"01d84752-ad12-4dfa-af63-65d12052a161"
            }
        }
    ]
}

答案 1 :(得分:0)

{