3

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>
1

7 Answers 7

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>

Sign up to request clarification or add additional context in comments.

Comments

2

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);
}

Comments

1

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

Comments

0

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>

2 Comments

It is recommended to use ng-options actually.
1. I never said anywhere that ng-repeat is recommended over ng-options 2. Why over complicate your code just to use recommended functionality...
0

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()

Comments

0

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.

Comments

0

Try this instead:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.