如何使用angularjs将对象显示到表中

时间:2015-03-17 03:44:12

标签: angularjs

我有这样的对象数据:

 var details = {  
            3:{
               2015-3-17 : 1,
               2015-3-18 : 0,
               routelines: "PASAY - CAGAYAN",
               tripcode: 3
              },
            4:{ 
               2015-3-17 : 0
               2015-3-18 : 4
               routelines: "PASAY - CAVITE",
               tripcode:4        
              },
 }

现在我打算将它们显示在表格中,但我不知道如何开始,因为它们都是对象。我想实现看起来像这样的输出:

tripcode | routlines | 2015-3-17 | 2015-3-18| 3 |PASAY - CAGAYAN | 1 | 0 | 4 |PASAY - CAVITE | 0 | 4 |

有人知道怎么做吗? 我试过这个,但遗憾的是它不起作用。

<div ng-repeat="detail in details">
  <div ng-repeat="(key, value) in detail">
      {{key}} : {{value}}
  </div>
</div>

2 个答案:

答案 0 :(得分:2)

确保正确格式化数据对象,某些键缺少引号。还应将详细信息绑定到$ scope。

试试这个:

&#13;
&#13;
function MyCtrl($scope) {
    
    $scope.details = {
        '3': {
            tripcode: 3,
            routelines: "PASAY - CAGAYAN",
            '2015 - 3 - 17': 1,
            '2015 - 3 - 18': 0
        },
        '4': {
           tripcode: 4,
           routelines: "PASAY - CAVITE",
           '2015 - 3 - 17': 0,
           '2015 - 3 - 18': 4
        },
    };
    
}
&#13;
.header,
.items {
 border-bottom: 1px solid #000;
 width: 600px;
}

.header span,
.items span {
    display: inline-block;
    width: 120px;
    border-right: 1px solid #000;
    padding: 5px;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app>
    <div ng-controller="MyCtrl">
        <div ng-repeat="detail in details">
            <div class="header" ng-show="$index == 0">
                <span ng-repeat="(key, value) in detail">{{key}}</span>
            </div>
            <div class="items">
                <span ng-repeat="(key, value) in detail">{{value}}</span>
            </div>
        </div>
    </div>
</div>
&#13;
&#13;
&#13;

答案 1 :(得分:1)

您错过了$scope

请将var details更改为$scope.details