我用select标签显示一系列年份(2014,2015,2016,2017)。我希望在显示我的页面时选择2014。
html:
<select ng-model="search.year" ng-options="year for year in years" null-is-undefined ng-selected="search.year">
<option value=""></option>
</select>
控制器:
budgetApp.controller('BudgetController', ['$scope', 'resolvedBudget', 'Budget', '$log',
function ($scope, resolvedBudget, Budget, $log) {
$scope.years = [2012,2013,2014,2015,2016,2017];
$scope.search.year = '2014'
{blablabla}
}]);
运行应用程序时,它说$ scope.search.year未定义,我不知道为什么。
答案 0 :(得分:2)
正如评论中所述,删除了ng-selected
<div ng-app='App'>
<div ng-controller="BudgetController">
<select ng-model="search.year" null-is-undefined ng-options="year for year in years" >
</select>
</div>
</div>
<强> see ng-selected Doc 强>
HTML
var budgetApp = angular.module('App', []);
budgetApp.controller('BudgetController', ['$scope', function ($scope) {
$scope.years = [2012,2013,2014,2015,2016,2017];
$scope.search = {year:2013};
}]);
控制器
{{1}}
答案 1 :(得分:1)
以下是解决方案http://jsbin.com/juxebewu/7/
<html ng-app='App'>
<body>
<div ng-controller="BudgetController">
<select ng-model="search.year" ng-options="year as year for year in years" ng-init="search.year=2014">
</select>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.14/angular.min.js"></script>
</body>
</html>
JS:
var budgetApp = angular.module('App', []);
budgetApp.controller('BudgetController', ['$scope', function ($scope) {
$scope.years = [2012,2013,2014,2015,2016,2017];
}]);