【问题标题】:AngularJS: ui-select once selected not able to remove selected option apart from change optionAngularJS:ui-select一旦选择除了更改选项之外无法删除选定的选项
【发布时间】:2016-09-25 05:20:47
【问题描述】:

我正在使用 ui-select 从服务器获取数据并将其填充到下拉列表中(搜索和选择)。我为你创建了一个plunker

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

一旦我从选择器中选择了任何值,我就可以进一步更改。但无法删除。我怎样才能做到这一点?

【问题讨论】:

  • '无法删除' 删除什么?选自select?还是从显示的选中项?
  • @devqon 从选择中选择
  • 我想像没有选择任何选项一样退缩
  • 然后重置country.selected? &lt;button ng-click="country.selected = null"&gt;Remove&lt;/button&gt;

标签: javascript angularjs ui-select


【解决方案1】:

您应该使用像select2 这样的其他主题来完成这项工作。我升级了你的PLUNKER 来展示它是如何工作的。将allow-clear="true" 添加到ui-select-match 并将主题设置为theme="select2" 以允许取消选择项目。

<ui-select ng-model="country.selected" 
           theme="select2" 
           ng-disabled="disabled">
      <ui-select-match allow-clear="true" placeholder="Select country">
            {{$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>

【讨论】:

  • 感谢您的回答@lin。它帮助我解决了项目中超过 12 个问题。
猜你喜欢
  • 2022-01-19
  • 1970-01-01
  • 2016-09-13
  • 1970-01-01
  • 1970-01-01
  • 2012-12-07
  • 2015-03-22
  • 1970-01-01
  • 2012-02-25
相关资源
最近更新 更多