如何在angularjs中的select选项中设置默认值?

时间:2015-08-25 19:32:35

标签: angularjs angularjs-controller angularjs-select2

<select id="marketSelect" ng-model="market" name="marketSelect" ng-change="update()" ng-options=" market.Label group by market.FYear for market in markets" class="form-control">  
</select> 

ID和标签存储在$cookieStore中。我尝试使用控制器中的代码

设置默认值
$scope.market = $cookie.get('marketID');

但它不起作用,我也试过ng-init

4 个答案:

答案 0 :(得分:0)

get返回一个字符串,在你的repeat语句中我可以看到你正在使用object,而不是put-get你可以尝试putObject-getObject吗?

答案 1 :(得分:0)

什么返回此行?

      $cookie.get('marketID')

我认为未定义,请检查。

答案 2 :(得分:0)

您可以通过这种方式解决:

控制器中的

$scope.markets = [
  {id:1,Label:'dd',FYear:2015},
  {id:2,Label:'pp',FYear:2014},
  {id:3,Label:'ss',FYear:2015},
  {id:4,Label:'kk',FYear:2014}
  ];

$scope.marketId = 1; // assume $cookie.get('marketID') get value = 1

在html中:

<select id="marketSelect" ng-model="marketId" name="marketSelect" ng-change="update()"  ng-options="market.id as market.Label group by market.FYear for market in markets"  class="form-control">

PLUNKER Demo Link

答案 3 :(得分:0)

通过功能

$scope.resetfunction= function() {
    $scope.countrylist= "";
}

<强> HTML

<select class="selectBox" ng-model="countrylist" ng-options="list for list in filterCountry | orderBy:'toString()'"><option value="">Select</option></select>