在angularjs中每X分钟生成一次数组

时间:2016-07-02 08:50:52

标签: javascript html angularjs angularjs-ng-repeat

我看到类似的问题,但我不知道如何处理angularjs。 这段代码返回一个数组,我希望在特定的预约时间内制作超链接标记。

html代码:<a> </a>

css代码:

a:link, a:visited {
background-color: white;
color: black;
padding: 9px 25px;
text-align: center;
text-decoration: none;
display: inline-block;
line-height: 0em;
}


a:hover, a:active {
background-color: #52D017;
}`

脚本:

var d = 12;

var n = 0,
min = 20,
periods = [" AM", " PM"],
times = [],
hours = [ 9, 10, 11,12, 5,6,7,8];

for (var i = 0; i < hours.length; i++) {
 times.push(hours[i] + ":" + n + n + periods[0]);
while (n < 60 - min) {
 times.push(hours[i] + ":" + ((n += 20) < 10 ? "O" + n : n) +  periods[0])
 }
 n = 0;
 }

times = times.concat(times.slice(0).map(function(time) {
 return time.replace(periods[0], periods[1])
}));


console.log(times);
document.querySelector("a").textContent += times.join()

请帮我改变angularjs中的代码并使用ng-reapeat并点击特定时间。

1 个答案:

答案 0 :(得分:4)

使用ng-repeat指令:

<div ng-repeat="v in times">
  <pre>{{v|json}}</pre> <!-- for demo only-->
</div>

Demo

固定代码(已将time替换为$scope.time):

var app = angular.module('myModule', []);

app.controller('fessCntrl', function($scope, $timeout) {

  var d = 12,
    n = 0,
    min = 20,
    periods = [" AM", " PM"],
    hours = [9, 10, 11, 12, 5, 6, 7, 8];

  $scope.times = [];

  for (var i = 0; i < hours.length; i++) {
    $scope.times.push(hours[i] + ":" + n + n + periods[0]);
    while (n < 60 - min) {
      $scope.times.push(hours[i] + ":" + ((n += 20) < 10 ? "O" + n : n) + periods[0])
    }
    n = 0;
  }

  $scope.times = $scope.times.concat($scope.times.slice(0).map(function(time) {
    return time.replace(periods[0], periods[1])
  }));
});