从角度控制器更改下拉选定值

时间:2014-11-06 11:00:54

标签: angularjs

我需要更改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 个答案:

答案 0 :(得分: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>

答案 1 :(得分:1)

设置初始选择的默认值:通过控制器中的$scope.selected设置 - 不要使用ng-init

要在数据库保存后重置默认值,您将要使用保存操作的保证并重置promise的返回功能中的选择字段。