2014-05-22 36 views
0

我使用select標記顯示年份數組(2014,2015,2016,2017)。我希望在顯示我的頁面時選擇2014年。angularjs:在打開html頁面時,不能自動選擇選擇標記中的選項

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

你能證明你的控制?你在打電話嗎? ng-controller =「...」 – Merlin

+0

我的控制器在html文件中工作正常,除了這個問題以外的所有內容。 – user1260928

+0

刪除'ng-selected' – tymeJV

回答

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

作爲評價所述除去ng-selected這對於OPTIONS see ng-selected Doc

WORKING FIDDLE

HTML

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

控制器

var budgetApp = angular.module('App', []); 

budgetApp.controller('BudgetController', ['$scope', function ($scope) { 
    $scope.years = [2012,2013,2014,2015,2016,2017]; 
    $scope.search = {year:2013}; 
}]); 
+0

感謝您的回答,對於那些使用這個更時尚的Select組件的用戶來說還有一個問題(http://silviomoreto.github.io/bootstrap-select)。我無法選中我的元素。如果碰巧有人在這裏使用它,並知道如何讓它工作...... – user1260928