问题:我有一个来自后端的值,需要在下拉列表中(它需要是默认的选定值)。下拉列表包含一组值,包括该值。但不知何故,即使在scoep中指定了该值,它也没有出现在前端。这是代码 HTML
<select class="bs-select form-control" ng-model="car.yearOfPurchase " id="year" name="year" required="">
<option ng-repeat="year in years" value="year">{{year}}</option>
</select>
JS
$scope.years = [];
for (var i = 2002; i <= parseInt(moment().format("YYYY")); i++) {
$scope.years.push(i.toString());
}
/*Value comign from backend. Mocked in this case.*/
$scope.car = {
yearOfPurchase : "2006"
};
$scope.year = $scope.car.yearOfPurchase;
另外,我为此创建了plunker。
答案 0 :(得分:2)
<select class="bs-select form-control" ng-model="car.yearOfPurchase"
id="year" name="year" required=""
ng-options="year for year in years">
</select>
请注意,ng-model
中有一个尾随空格。
我切换到ng-options而不是ng-repeat选项。