2016-04-13 55 views
1

我想创建表来显示从API接收的数据。数据采用JSON格式,需要使用AngularJS在桌面上显示。我可以在视图中检索所需的数据,但我无法在表格中正确格式化。该代码是在plunker这里:https://plnkr.co/edit/GZtWlx5aDWvsseSs9ugW?p=preview如何使用angularjs在表格中正确格式化JSON数据?

查看

<body ng-app="ShopOpeningHrs"> 
<div class="container" ng-controller="ScheduleCtrl"> 
    <table> 
     <thead> 
     <tr> 
      <th>Sunday</th> 
      <th>Monday</th> 
      <th>Tuesday</th> 
      <th>Wednesday</th> 
      <th>Thursday</th> 
      <th>Friday</th> 
      <th>Saturday</th> 
     </tr> 
     </thead> 
     <tbody> 
     <tr ng-repeat="schedule in singledayschedule"> 
      <td>{{ schedule.morning }}</td> 
      <td>{{ schedule.evening }}</td> 
     </tr> 
     </tbody> 
    </table> 
</div> 

var app = angular.module("ShopOpeningHrs", []); 
    app.controller("ScheduleCtrl", function($scope, $http) { 
    $http.get('data.json'). 
    success(function(data, status, headers, config) { 
     $scope.schedule = data[0]; 
     $scope.singledayschedule = []; 

     angular.forEach($scope.schedule, function(value, key) { 
      angular.forEach(value, function(value, key) { 
       $scope.singledayschedule.push(value); 
      }); 
     }); 
    console.log($scope.singledayschedule); 
    }). 
    error(function(data, status, headers, config) { 
    // log error 
    }); 
}); 

回答

1

我想你只需要使用两个ngRepeat循环:

<tbody> 
    <tr ng-repeat="(shop, schedule) in singledayschedule"> 
     <td>{{ shop }}</td> 
     <td ng-repeat="(day, times) in schedule"> 
     <div>{{ times.morning }}</div> 
     <div>{{ times.evening }}</div> 
     </td> 
    </tr> 
    </tbody> 

德mo:https://plnkr.co/edit/CYSqQYFhENQfeeYRFTvp?p=preview