如何使用angularjs正确格式化表中的JSON数据
How to properly format JSON data in table using angularjs?
我正在尝试创建一个表来显示从API接收的数据。数据采用JSON格式,需要使用AngularJS在表中显示。我可以在视图中检索所需的数据,但无法在表中正确格式化。代码在plunker中:https://plnkr.co/edit/GZtWlx5aDWvsseSs9ugW?p=preview
查看
<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
});
});
我认为您只需要使用两个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>
演示:https://plnkr.co/edit/CYSqQYFhENQfeeYRFTvp?p=preview
相关文章:
- 如何将JSON数据导入我的ejs模板
- 如何将json数据显示为html
- Json数据包含日期和时间格式
- 用我的json数据填充JQuery DataTable
- AngularJS加载JSON数据,然后从中解析/加载HTML
- 使用jquery读取Json数据
- 如何将本地json数据加载到Extjs数据模型中
- 如何使用jquery ajax和jsonp在您自己的域上读取json数据
- React-将jSON数据传递给子组件的子组件
- 尝试从控制器加载带有json数据的Simile时间线
- Jquery:对返回JSON数据的php脚本的Ajax调用
- 如何通过数据分组将json数据转换为嵌套的json数据格式
- 如何在ReactJs中显示Json数据
- 使用javascript进行JSON数据检索
- tu如何将id放在填充了json数据的html表td上
- 将JSON数据从服务器加载到knockout.jsobservable中
- 使用JavaScript处理JSON数据
- 如何将json数据传递到.js文件
- 如何在带有nunjucks和gullow数据的JSON数据中使用带有HTML的内容
- 如何使用var名称调用JSON数据