重复日期数组并不显示网页中的数据,请注意,当我将数据直接设置到数组而没有循环时,它可以工作:
在此代码中,我将字符串日期转换为对象,然后创建日期数组,在每次迭代中增加1天的日期:
//dates for table
//convert date to object to allow me do action on it like increase the date in the table
var startDate = $scope.tripById[0].start_date;
var dateInNumberFormat = new Date(startDate).getTime();
//create array for the dates to show it on table, each cell will have 1 extra day
var day = 1000 * 3600 * 24; //day in miliseconds 1000 * 3600 = hour
$scope.dates[0] = new Date(dateInNumberFormat + day);
for(var i = 1; i < daysSum ; i++){
day = 1000 * 3600 * (i * 24);
$scope.dates[daysSum] = new Date(dateInNumberFormat + day);
console.log($scope.dates[daysSum]);
}
这是ng-repeat:
<table ng-table="usersTable" class="table table-striped" style="text-align: center;">
<tr>
<td>Date</td>
<td ng-repeat="date in dates track by $index">{{date}}</td>
</tr>
为什么在我的阵列上重复ng不会显示网页上的日期?
答案 0 :(得分:1)
在循环中,您将所有日期放在同一索引中。将其更改为:
for(var i = 1; i < daysSum ; i++){
day = 1000 * 3600 * (i * 24);
$scope.dates[i] = new Date(dateInNumberFormat + day);
console.log($scope.dates[i]);
}