2015-11-02 107 views
0

我想在angularjs的下拉列表中顯示日期,月份和年份。 但我不能找到一個解決方案循環顯示日期angularjs

這裏是我的代碼

<div ng-controller="MyCtrl"> 
<label>Select date</label> 
<select ng-model="date"> 
<option value="12">12</option> 
</select> 
<label>Select month</label> 
<select ng-model="month"> 
<option value="12">12</option> 
</select> 
<label>Select year</label> 
<select ng-model="year"> 
<option value="2015">2015</option> 
</select> 

我想在ng-repeat顯示上述三個drpodowns顯示 從1990年12月12日範圍爲12-12-日期2020 我該怎麼做?

+0

您可以使用角日期時間(https://github.com/eight04/ angular-datetime) –

回答

0

您可以通過爲日期的不同元素(從1900年到2015年31天,12個月和年)定義一個範圍,但我不會建議這樣做。

你永遠不能確保這些值是有效的。 2月28日,29日,30日怎麼樣?

使用日期選擇器,它對用戶來說也是一個更好的體驗。

0

這是你想要實現的嗎?

angular.module('app', []) 
 
    .controller('MyCtrl', function($scope) { 
 
    $scope.date = '12'; 
 
    $scope.month = '12'; 
 
    $scope.year = '1990'; 
 

 
    $scope.getYearRange = function(start, end) { 
 
     // create empty array 
 
     var range = new Array(end - start + 1); 
 

 
     // fill the array 
 
     angular.forEach(range, function(item, index) { 
 
     range[index] = start + index; 
 
     }); 
 

 
     return range; 
 
    }; 
 
    });
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> 
 

 

 
<div ng-app="app" ng-controller="MyCtrl"> 
 
    <label>Select date</label> 
 
    <select ng-model="date"> 
 
    <option value="12">12</option> 
 
    </select> 
 
    <label>Select month</label> 
 
    <select ng-model="month"> 
 
    <option value="12">12</option> 
 
    </select> 
 
    <label>Select year</label> 
 
    <select ng-model="year"> 
 
    <option value="{{year}}" ng-repeat="year in getYearRange(1990, 2020)">{{year}}</option> 
 
    </select>

,如果你想更復雜的東西通常被稱爲日期選擇器,我認爲你應該使用ui-bootstrap datepicker

+0

yeahh正是:) –