【问题标题】:Is it possible to use single select but get model as an array是否可以使用单选但将模型作为数组获取
【发布时间】:2016-06-02 21:53:02
【问题描述】:

我正在使用 angular-ui 的 select2 组件,无论我使用单选还是多选,我都希望我的模型具有一致的值。

目前,如果我使用单选,模型只是设置为一个值,当使用多选时,模型是一个值数组。

IE单选:(非数组模型)

<ui-select ng-model="model.value" theme="bootstrap">
  <ui-select-match>{{$select.selected.title}}</ui-select-match>
  <ui-select-choices repeat="choice.title as choice in choices | filter: $select.search">
    <div ng-bind-html="choice.title | highlight: $select.search"></div>
  </ui-select-choices>
</ui-select>

或多:(数组模型)

<ui-select multiple ng-model="model.value" theme="bootstrap">
  <ui-select-match>{{$item.title}}</ui-select-match>
  <ui-select-choices repeat="choice.title as choice in choices">
    <div ng-bind-html="choice.title"></div>
  </ui-select-choices>
</ui-select>

我尝试使用多个,并添加 length="1" 属性,但是仍然将多选样式应用于选择框。

即使在单选时也可以从 select2 中获取数组模型吗?

【问题讨论】:

    标签: angularjs angular-ui select2


    【解决方案1】:

    如何通过将 ng-model="model.value" 更改为 ng-model="model.value[0]" 将模型设置为数组中的第一个位置?我相信这应该有效。如果没有,请先尝试将控制器中的模型初始化为空数组。 ($scope.model = [];)

    <ui-select ng-model="model.value[0]" theme="bootstrap">
      <ui-select-match>{{$select.selected.title}}</ui-select-match>
      <ui-select-choices repeat="choice.title as choice in choices | filter: $select.search">
        <div ng-bind-html="choice.title | highlight: $select.search"></div>
      </ui-select-choices>
    </ui-select>
    

    【讨论】:

    • 好主意!
    猜你喜欢
    • 1970-01-01
    • 2019-10-28
    • 2021-05-12
    • 1970-01-01
    • 2021-01-11
    • 1970-01-01
    • 2012-05-08
    • 2014-04-14
    • 1970-01-01
    相关资源
    最近更新 更多