2016-07-02 23 views
1

我看到類似的問題,但我不知道如何在angularjs中處理這個問題。 此代碼返回給我一個數組,我想在特定的時間製作超鏈接標記以預約書籍。在angularjs中生成每X分鐘的時間陣列

的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並點擊特定的時間。

回答

4

使用ng-repeat指令爲:

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

Demo


固定碼(與$scope.time取代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]) 
    })); 
}); 
+0

但是當我在我的控制器沒有運行該代碼將發生http://jsfiddle.net/y7hyheaa/2/ –

+0

與控制器混淆如何處理 –

+0

@AshishGoyal,它的一個Angular基礎,建議你從輔導材料開始:)。反正我回答你的問題 –