S
S
slip312014-08-05 11:09:52
JavaScript
slip31, 2014-08-05 11:09:52

How to make an array in option in angular.js?

Something I google badly today
. I need dates from 1950 to the present year in select. Naturally, not from
{value: 1, text: '1950'},
{value: 2, text: '1951'},
{value: 3, text: '1952'}
];, but by some loop in function
$ scope.showYear = function() {
var selected = $filter('filter')({value: $scope.form.year});
return some kind of loop? selected[0].text : 'empty';
};
How can I do this, given that the last year must be the real one? Thanks

Answer the question

In order to leave comments, you need to log in

3 answer(s)
R
Rainum, 2014-08-05
@slip31

The question is clear, the example code is not very good. Why an object with an index for the year? Wouldn't it be easier to store the year in value for option, and not its index?
I got the following code:
Controller:

function listYears(startYear) {
  var currentYear = new Date().getFullYear();
  var years = [];
  startYear = startYear || 1980;

  while (startYear <= currentYear) {
    years.push(startYear++);
  }

  return years;
}

$scope.years = listYears(1950);

Sample:
<select>
  <option ng-repeat="year in years" value="{{ $index }}">{{ year }}</option>
</select>

D
Damir Makhmutov, 2014-08-06
@doodoo

I'll add @Rainum :

<select ng-options="year for year in years"></select>

S
slip31, 2014-08-06
@slip31

Yes thank you. Everything is great. (the code was old from xeditable, just stuck it there). Certainly. value needs a year.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question