如何使用angular

时间:2016-06-10 17:49:48

标签: angularjs json html5

我的json中的数据是动态的,我希望在包含两列的表中显示数据。我正在尝试但只有前两个记录正在重复。我没有看到表中的所有记录..有什么帮助吗?

http://plnkr.co/edit/Hnb7hkjA16XDbzRT8VAt?p=preview

This is my json :  var data = '{  
   "output":{  
      "service-status":[  
         {  
            "service":"db",
            "service-name":"Mongo DB"
         },
         {  
            "service":"license",
            "service-name":"Smart License"
         },
         {  
            "service":"BRM",
            "service-name":"Billing"
         },
         {  
            "service":"subscription",
            "service-name":"subscription"
         }
      ]
   }
}';

我的HTML代码:

<table  border="1px" width="100%" ng-repeat="data in serviceData" ng-if="$index % 2 == 0">
     <tr>
         <td>{{serviceData[index]["service-name"]}}</td>
      </tr>
</table>

i want to display something like this

Mongo Db       Smart License
Billing        subscription

3 个答案:

答案 0 :(得分:0)

转换控制器中的数据:

var serviceDataView = function() {
    var res = [];
    for (var i = 0; i < $scope.serviceData.length; i+=2){
      res.push({
        col1: $scope.serviceData[i]['service-name'], 
        col2: $scope.serviceData[i+1] ? $scope.serviceData[i+1]['service-name'] : null
      });
    }
    return res;
};

$scope.structuredData = serviceDataView();

因此可以在视图中轻松使用:

<table border="1px" width="100%">
    <tr ng-repeat="data in structuredData">
        <td>{{data.col1}}</td>
        <td>{{data.col2}}</td>
    </tr>
</table>

Plunker

答案 1 :(得分:0)

使用ng-repeat的迭代器是$ index。

其他迭代者:https://docs.angularjs.org/api/ng/directive/ngRepeat

将表替换为:

<table  border="1px" width="100%" ng-repeat="data in serviceData">
    <tr>
        <td>{{serviceData[$index]["service"]}}</td>
        <td>{{serviceData[$index]["service-name"]}}</td>
    </tr>
</table>

答案 2 :(得分:0)

根据我的评论,此问题根本不需要$ index。您可以对表格使用过滤器ngRepeat.check [link] http://plnkr.co/edit/Hnb7hkjA16XDbzRT8VAt?p=preview

html code here: 
<table  border="1px" width="100%" ng-repeat="data in serviceData |limitTo:2 ">
 <tr>
     <td>{{data["service-name"]}}</td>
     <td>{{data.service}}</td>
</tr>