AngularJS - 计算JSON数组的长度

时间:2014-09-01 13:16:30

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

我有一组JSON数据,在其中,我正在尝试计算数组的长度(Infos):

我的JSON:

    "Association": {
    "Items": [
         {
            "AssocName": {
                "SurName": "BLOGGS",
                "Title": "MR",
                "ForeName": "HAPPY"
            },
            "Details": [
                {
                  "Name": {
                        "SurName": "BLOGGS",
                        "Title": "MR",
                        "ForeName": "HAPPY"
                    },
                   "Infos": [
                      {
                         //Some Data
                         //Some Data
                         //Some Data
                      },
                      {
                         //Some Data
                         //Some Data
                         //Some Data
                      },
                      {
                         //Some Data
                         //Some Data
                         //Some Data
                      },
                      {
                         //Some Data
                         //Some Data
                         //Some Data
                      }
                    ]
                 }
             ]
        },
        {
            "AssocName": {
                "SurName": "BLOGGS",
                "Title": "MRS",
                "ForeName": "BLUE"
            },
            "Details": [
                {
                  "Name": {
                        "SurName": "BLOGGS",
                        "Title": "MRS",
                        "ForeName": "BLUE"
                    },
                   "Infos": [
                      {
                         //Some Data
                         //Some Data
                         //Some Data
                      },
                      {
                         //Some Data
                         //Some Data
                         //Some Data
                      }                     
                   ]
               }
            ]
        }
    ]
}

我的HTML:

<tr ng-repeat-start="associations in Association.Items track by $index">
    <td>
        {[{associations.AssocName.Title}]}
        {[{associations.AssocName.ForeName}]}
        {[{associations.AssocName.SurName}]}
    </td>
    <td>
        {[{ associations[$index].Details.Infos.length }]} of Details
    </td>
</tr>

但似乎没有显示任何价值......标题,姓名和姓氏都按预期显示。

2 个答案:

答案 0 :(得分:1)

试试这种方式,如下所示

<强> Working Demo

<table>
<tr ng-repeat="associations in Association.Items">
    <td>
        {{associations.AssocName.Title}}
        {{associations.AssocName.ForeName}}
        {{associations.AssocName.SurName}}
    </td>
    <td>
        [{{associations.Details[0].Infos.length}}] of Details
    </td>
</tr>
</table>

答案 1 :(得分:-1)

您不需要索引

<tr ng-repeat-start="associations in Association.Items">
    <td>
        {[{associations.AssocName.Title}]}
        {[{associations.AssocName.ForeName}]}
        {[{associations.AssocName.SurName}]}
    </td>
    <td>
        {[{ associations.Details.Infos.length }]} of Details
    </td>
</tr>