2014-11-06 101 views
1

我需要從db中的已保存值中更改下拉列表的「選定值」。我怎樣才能做到這一點?從角度控制器中更改下拉選擇的值

控制器

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

app.controller('ctrl', function($scope) { 
    $scope.months = ['January','February','March','April','May','June','July','August','September','October','November','December']; 

    $scope.selected='August';//here it's not working ? 
}); 

HTML

<html ng-app="app"> 
    <body ng-controller="ctrl"> 
<div class="sample" align="center"> 
    <h1>AngularJS Combobox</h1> 
<select ng-model="selected" ng-options="opt as opt for opt in months" ng-init="selected='March'"></select> 
    <h3>You have selected : {{selected}}</h3> 
</div> 
    </body> 
</html> 

My CODE PEN is here.

任何幫助將高度讚賞。

回答

2

刪除:

ng-init="selected='March'" 

這樣你的新視圖代碼如下:

<html ng-app="app"> 
    <body ng-controller="ctrl"> 
<div class="sample" align="center"> 
    <h1>AngularJS Combobox</h1> 
<select ng-model="selected" ng-options="opt as opt for opt in months"></select> 
    <h3>You have selected : {{selected}}</h3> 
</div> 
    </body> 
</html> 
+0

那麼我怎樣才能給一個默認的負載值? – Sampath 2014-11-06 11:11:27

+1

您正在使用兩種競爭的默認聲明方法。只需使用一個 - 刪除ng-init並使用$ scope.selected選擇默認值,或者,不要完全刪除ng-init,只需將它的值更改爲'August'即可。 – 2014-11-06 11:13:01

+0

你跑過我給你看的東西了嗎? 'ng-model =「selected」'將默認加載值設置爲您在'$ scope.selected ='August'中設置的值;' – 2014-11-06 11:13:50

1

設置初始選擇的默認連接:$scope.selected控制器設定 - 不要使用ng-init爲了這。

要在數據庫保存後重置默認值,您需要使用保存操作的承諾並重置承諾的返回功能中的選擇字段。

+0

+1,感謝支持:) – Sampath 2014-11-06 11:42:19

相關問題