AngularJS ui-select下拉默认值

时间:2015-10-07 03:02:25

标签: javascript angularjs ui-select2

我正在使用angular ui-select下拉列表。如何为下拉值设置默认值?

<h3>Selectize theme</h3>
  <p>Selected: {{country.selected}}</p>
  <ui-select ng-model="country.selected" theme="selectize" ng-disabled="disabled" style="width: 300px;">
    <ui-select-match placeholder="Select or search a country in the list...">{{$select.selected.name}}</ui-select-match>
    <ui-select-choices repeat="country in countries | filter: $select.search">
      <span ng-bind-html="country.name | highlight: $select.search"></span>
      <small ng-bind-html="country.code | highlight: $select.search"></small>
    </ui-select-choices>
  </ui-select>

该代码段来自plnkr.co。目前下拉列表只播放默认值Select or search a country in the list...但我需要从控制器传递一个值。让我们说$scope.default = {"name":"Decard"}

谢谢!

修改

此问题与This one类似,但涉及json返回数据格式。

1 个答案:

答案 0 :(得分:27)

您可以将ng-model初始化为控制器中的默认国家/地区对象,如下所示:

 $scope.country = {};
 $scope.country.selected = {name: 'Albania', code: 'AL'};

然后使用“ui-select-match”设置默认值,如下所示:

<ui-select ng-model="country.selected" theme="selectize" ng-disabled="disabled" style="width: 300px;">
<ui-select-match placeholder="Select or search a country in the list...">{{ $select.selected.name }}</ui-select-match>
<ui-select-choices repeat="country in countries | filter: $select.search">
  <span ng-bind-html="country.name | highlight: $select.search"></span>
  <small ng-bind-html="country.code | highlight: $select.search"></small>
</ui-select-choices>
</ui-select>