AngularJS ui-select下拉式默认值

AngularJS ui-select dropdown default value

本文关键字:默认值 ui-select AngularJS      更新时间:2023-09-26

我使用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"}

谢谢! !

编辑

这个问题与这个问题类似,但涉及json返回格式的数据

您可以将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>