2017-04-15 171 views
0

我有一個小問題,我只是需要有我的第一個元素在我的選擇,就目前我剛纔白排在第一個選項,選擇選擇angularjs

這是我的一點代碼

<select style="border-radius: 4px;" class="form-control" ng-model="select" ng-init="somethingHere = option[1]" id="sel1"> 
       <option ng-repeat="option in dateHeureAdd track by option.value" value="{{option.value}}">{{option.heure}}</option> 
      </select> 

CTRL

$scope.dateHeureAdd = [ 

    {value: '8', heure: '08:00'}, 
    {value: '9', heure: '09:00'}, 
    {value: '10', heure: '10:00'}, 
    {value: '11', heure: '11:00'}, 
    {value: '12', heure: '12:00'}, 
    {value: '13', heure: '13:00'}, 
    {value: '14', heure: '14:00'}, 
    {value: '15', heure: '15:00'}, 
    {value: '16', heure: '16:00'}, 
    {value: '17', heure: '17:00'} 

         ] 

這裏是一個plunker

http://plnkr.co/edit/aDUGvrvJjszq07Sjh0TI?p=preview

感謝您的幫助

回答

2

有針對被稱爲ng-options選擇特殊的指令,還你能夠參考ng-module的名稱,並設置它的默認。

這裏的工作液:http://plnkr.co/edit/eHMoEqVxTv5QQh12FWv1?p=preview

的Index.html:

<!doctype html> 
<html ng-app="ui.bootstrap.demo"> 

<head> 
    <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.6.1/angular.js"></script> 
    <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.6.1/angular-animate.js"></script> 
    <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.6.1/angular-sanitize.js"></script> 
    <script src="//angular-ui.github.io/bootstrap/ui-bootstrap-tpls-2.5.0.js"></script> 
    <script src="//cdnjs.cloudflare.com/ajax/libs/angular-ui-router/0.4.2/angular-ui-router.js"></script> 

    <script src="script.js"></script> 
    <link href="//netdna.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"> 
</head> 

<body> 
    </br> 
    <div ng-controller="Ctrl"> 
    <select class="form-control" ng-model="mySelect" ng-options="date as date.heure for date in dates track by date.value"> 

    </select> 
    </div> 
</body> 

</html> 

的script.js:

angular.module('ui.bootstrap.demo', ['ui.router','ngAnimate', 'ngSanitize', 'ui.bootstrap']); 



angular.module('ui.bootstrap.demo', []) 
    .controller('Ctrl',[ '$scope', function ($scope) { 
    $scope.dates = [ 
    {value: '8', heure: '08:00'}, 
    {value: '9', heure: '09:00'}, 
    {value: '10', heure: '10:00'}, 
    {value: '11', heure: '11:00'}, 
    {value: '12', heure: '12:00'}, 
    {value: '13', heure: '13:00'}, 
    {value: '14', heure: '14:00'}, 
    {value: '15', heure: '15:00'}, 
    {value: '16', heure: '16:00'}, 
    {value: '17', heure: '17:00'}] 

    $scope.mySelect = $scope.dates[0]; 
}]); 
0

你表達了你的初始化選擇值是錯誤的;你不應該在ng-repeat中使用別名,而應該使用原始數組的名稱。

ng-init="somethingHere = option[1]" 

ng-init="somethingHere = dateHeureAdd[1].value" 

此外,您必須使用選擇的NG-模型分配給,這是你的情況不同。

Corrected Example