我正在使用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...,但我需要从 Controller 传递一个值。可以说$scope.default = {"name":"Decard"}
谢谢!!

编辑

这个问题类似于This one,但涉及数据的json返回格式。

最佳答案

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

 $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>

09-25 17:43
查看更多