Answer the question
In order to leave comments, you need to log in
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
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);
<select>
<option ng-repeat="year in years" value="{{ $index }}">{{ year }}</option>
</select>
I'll add @Rainum :
<select ng-options="year for year in years"></select>
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question