2014-02-07 38 views
0

我一直在嘗試使用ng選項創建DOB下拉菜單。我有一些如何創建它的日期爲&個月,但在創建年份時,我面臨的問題是,我的下拉菜單正確顯示年份的順序相反,即從2014年 - 1915年開始,但值屬性顯示值從1到100而不是2014年 - 1915年,你能否提出一個更好的方法來做到這一點。我發現這種方式有點跛腳。我不想使用NG重複出生日期使用ng選項下拉菜單

HTML

<select class="day" id="DateOfBirth_Day" name="DateOfBirthDay" required ng-pattern="\d+" ng-model="user.question5.answer.dobday" ng-options="o for o in days"> 
    <option value="DD">DD</option> 
</select> 
<select class="month" id="DateOfBirth_Month" name="DateOfBirthMonth" required ng-pattern="\d+" ng-model="user.question5.answer.dobmonth" ng-options="o for o in months"> 
    <option value="MM">MM</option> 
</select> 
<select class="year" id="DateOfBirth_Year" name="DateOfBirthYear" required ng-pattern="\d+" ng-model="user.question5.answer.dobyear" ng-options="o for o in years"> 
    <option value="YYYY">YYYY</option> 
</select> 

控制器代碼

$scope.user = { 
    question5: { 
     answer: { 
      dobday: 'DD', 
      dobmonth: 'MM', 
      dobyear: 'YYYY' 
     } 
    } 
} 
$scope.totaldays = 31; 
$scope.totalmonths = 12; 
$scope.totalyears = 100; 
$scope.days = ['DD']; 
for (var i = 0; i < $scope.totaldays; i += 1) { 
    $scope.days.push(i + 1); 
} 

$scope.months = ['MM']; 
for (var i = 0; i < $scope.totalmonths; i += 1) { 
    $scope.months.push(i + 1); 
} 

$scope.years = ['YYYY']; 
for (var i = 2014; i > 2014 - $scope.totalyears; i--) { 
    $scope.years.push(i - 1); 
} 

回答

0

當然,你可以使用ng-repeat<option>標籤,而不是ng-options

<select class="year" id="DateOfBirth_Year" name="DateOfBirthYear" required ng-pattern="\d+" ng-model="user.question5.answer.dobyear" > 
    <option value="YYYY">YYYY</option> 
    <option ng-repeat="o in years" value="{{o}}">{{o}}</option> 
</select> 

演示Fiddle

+0

我其實新的角度,只是想知道最好的做法也是如此,則建議使用NG-重複,而不是NG-選項時,實際上我們正在創造一個選擇標籤 – Achyut

+0

吧,如果你想要添加'value'或'title',使用上面提到的方式 –

相關問題