2017-01-10 29 views
3

我有一個下拉菜單和文本框,用於計算特定日期的時間,在文本框中輸入持續時間,根據該時間段在下拉菜單中生成時間段-下。這適用於一個文本框和一個下拉菜單,但是對於多個文本框和下拉菜單,它將使用分配給第一個文本框的相同值。那麼,如何獲得每天的多個值以及錯誤是什麼。爲了顯示多個文本框和下拉菜單,我使用了ng-repeat。請幫助我,如果有什麼不對,請糾正我。謝謝:)使用多個文本框和下拉列表

HTML:

<tr ng-repeat="field in fields" name="slotSelection"> 
<td align="center"> 
<ion-checkbox ng-model="field.checked">{{field.name}} </ion-checkbox> 
</td> 
<td> 
<select ng-model="time" ng-options="time for time in timeslots"> 
<option value="">select</option> 
</select> 
</td> 
<td> 
<input type="text" ng-model="interval" ng-blur="setTimeSlots(interval)"> 
</td> 
</tr> 

控制器:

$scope.fields = [{ id: 1, name: 'Sunday' }, 
     { id: 2, name: 'Monday' }, 
     { id: 3, name: 'Tuesday' }, 
     { id: 4, name: 'Wednesday' }, 
     { id: 5, name: 'Thursday' }, 
     { id: 5, name: 'Friday' }, 
     { id: 5, name: 'Sunday' }] 

     $scope.setTimeSlots = function (interval) { 
     var startingTime = moment().hours(8).minutes(0); 
     $scope.timeslots = []; 
     for(var i=0; i < interval; i++){ 
     $scope.intervals=60; 
      $scope.timeslots.push(startingTime.add($scope.intervals,'minute').format("h:mm")); 
    } 
    } 

回答

1

你必須在timeslots添加到每個fieldfields scope屬性,你可以通過當前fieldsetTimeSlot方法如圖所示,

<input type="text" ng-model="interval" ng-blur="setTimeSlots(interval, field)"> 

然後在setTimeSlots,按時段,如下圖所示,

$scope.setTimeSlots = function(interval, field) { 
    var startingTime = moment().hours(8).minutes(0); 
    field.timeslots = []; 
    for (var i = 0; i < interval; i++) { 
     $scope.intervals = 60; 
     field.timeslots.push(startingTime.add($scope.intervals, 'minute').format("h:mm")); 
    } 
    } 

工作plunkr here

+0

我將能夠讀取整行的時候,天檢查? – user7397693

+0

你的意思是'天'被檢查'? 'setTimeSlots'函數總是會從'fields'屬性 – Thaadikkaaran

+0

中接收間隔和當前'field'對象,因爲有一個複選框,如果用戶想要顯示選定的日子以及持續時間和時隙,我將能夠獲得它,即json顯示'[{'day':星期一,'duration':3,'selectedSlot:11},{'day':星期三,'duration':2,'selectedSlot':9}]' – user7397693