最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Angular Create Dynamic year dropdown - Stack Overflow

programmeradmin0浏览0评论

I want to create a year selection dropdown. year must start from this year upto next 7 years .

I have tried

var year = new Date().getFullYear();
var range = [];

range.push(year);

for (var i = 1; i < 7; i++) {
    range.push(year + i);
}

$scope.years = range;

<select class="form-control"  ng-options="year for year in years" name="expiryMonth" ng-model="expiryMonth" required>'

But this creates a html ,

<option label="2016" value="number:2016">2016</option>

this creates a required year dropdown But i want Value to be last two digits of label

Also, my first option should be like

<option value="">select month</option>

I want to create a year selection dropdown. year must start from this year upto next 7 years .

I have tried

var year = new Date().getFullYear();
var range = [];

range.push(year);

for (var i = 1; i < 7; i++) {
    range.push(year + i);
}

$scope.years = range;

<select class="form-control"  ng-options="year for year in years" name="expiryMonth" ng-model="expiryMonth" required>'

But this creates a html ,

<option label="2016" value="number:2016">2016</option>

this creates a required year dropdown But i want Value to be last two digits of label

Also, my first option should be like

<option value="">select month</option>
Share Improve this question edited Nov 23, 2016 at 10:22 monda asked Nov 23, 2016 at 9:37 mondamonda 3,91516 gold badges61 silver badges85 bronze badges 1
  • Don't understand. Value of your model is the same as label. See this Plunker plnkr.co/edit/ygxpfIlheIhGE8MvUIIR?p=preview – gauss Commented Nov 23, 2016 at 10:12
Add a comment  | 

7 Answers 7

Reset to default 7

angular.module('app', [])
    .controller("Ctrl",
    function contactListCtrl($scope) {

       var year = new Date().getFullYear();
    var range = [];
    range.push(year);
    for (var i = 1; i < 7; i++) {
        range.push(year + i);
    }
    $scope.years = range;


    });
<!DOCTYPE html>
<html ng-app="app">

<head>
    <link data-require="[email protected]" data-semver="3.3.6" rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.css" />
    <script data-require="[email protected]" data-semver="1.4.9" src="https://code.angularjs.org/1.4.12/angular.js"></script>

    <script src="script.js"></script>
</head>

<body ng-controller="Ctrl">

<select class="form-control" ng-model="expiryMonth" required>
  <option>Select month</option>
  <option ng-repeat="year in years">{{year}}</option>
  </select>
</body>

</html>

This will give you a useful dynamic year range.

let years: number[] = [];
let currentYear: number = new Date().getFullYear();
for(let i = (currentYear - 7); i < (currentYear + 7); i++) {
    years.push(i);
}

If you want that value will be only last two digits of label you can extend your years object like:

var year = new Date().getFullYear();
var range = [];

for (var i = 0; i < 7; i++) {
    range.push({
      label: year + i,
      value: parseInt(String(year + i).slice(2, 4))
    });
}

$scope.years = range;

And ng-options like:

ng-options="year.value as year.label for year in years"

See this Plunker

Consider using ng-repeat to create your <option>

<select class="form-control" name="expiryMonth" ng-model="expiryMonth" required>
    <option value="">select month</option>
    <option ng-repeat="year in years" value="{{year}}">{{year}}</option>
</select>

For dates and times in angular i often use:

'uib-datepicker'

More information: https://angular-ui.github.io/bootstrap/

In this directive you can use 'min-date'

 <input type="text" starting-day="0"  uib-datepicker="{{format}}" ng-model="vm.dateFrom" is-open="true" min-date="vm.minDate" max-date="vm.dateTo"  />

And in your controller:

vm.minDate = yourFunctionToDate()

I made you a fiddle that you can find here

Basically, use the expression track by to give the value you want to your options.

Try this instead:

<select ng-model="expiryMonth">
    <option ng-repeat="year in years" value="{{year}}">{{year}}</option>
</select>
发布评论

评论列表(0)

  1. 暂无评论