Ashish Goyal Ashish Goyal - 5 months ago 10
HTML Question

Generate array of times for every X minutes in angularjs

i see the similar question but i didn't know how to deal this in angularjs.
This code return me a array and i want to make hyperlink tag in particular time for book appointment.

html code:

<a> </a>


css code:

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;
}`


script:

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()


please help me out to change this code in angularjs and using ng-reapeat and click on particular time.

Answer

Use ng-repeat directive as:

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

Demo


Fixed code (replaced time with $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])
  }));
});
Comments