【问题标题】:Angular UI Select Options issueAngular UI 选择选项问题
【发布时间】:2016-06-10 15:54:06
【问题描述】:

我正在尝试使用角度 UI 选择,但遇到了问题。当我的表单加载时,选项似乎正确填充,但它没有从我的模型中选择的值。例如,下面的屏幕截图显示了一个具有优先级 (2) 正常选择的项目,但正如您所见,当表单加载时它似乎是空的。

然后,如果我选择其中一个选项并保存它会尝试使用整个对象

{"text":"(2) Normal","value":"(2) Normal"} 

而不仅仅是值,但顶部仍然显示为空。

<ui-select ng-model="currentItem.PriorityValue" theme="bootstrap" style="min-width: 300px;" title="Choose a priority">
    <ui-select-match>{{$select.currentItem.PriorityValue}}</ui-select-match>
    <ui-select-choices repeat="option in (data.priority.options |  filter: $select.search) track by option.value">
        <span ng-bind="option.text"></span>
    </ui-select-choices>
</ui-select>

编辑:如果它有帮助,这就是我之前所做的工作,但现在我需要选择的过滤功能

<select class="form-control modal-input" id="PriorityValueInput" data-ng-options="option.value as option.text for option in data.priority.options" data-ng-model="currentItem.PriorityValue"></select>

【问题讨论】:

  • 重复尝试option.value as option in (data.priority.options | filter: $select.search) track by option.value
  • 感谢乔治这有帮助。数据现在正确地保存到服务器,就像 (2) Normal 但是,选择框仍然只显示为空,而不是显示当前选择的选项。
  • 我设法通过将“{{$select.currentItem.PriorityValue}}”更改为“{{currentItem.PriorityValue}}”。现在我还有另一个问题。当您输入过滤器然后删除您输入的内容时,当前选择的选项会消失。是否有一个选项可以使它不从下拉列表中删除选项,即使它们已经被选中?

标签: angularjs angular-ui-select


【解决方案1】:

我尝试在下面的 plunker 中做同样的事情。希望这会对你有所帮助。

http://plnkr.co/edit/BQuiDDULwVJgRRzlRx4x?p=preview

<ui-select ng-model="ctrl.country.selected" theme="selectize" ng-disabled="ctrl.disabled" style="width: 300px;" title="Choose a country">
<ui-select-match placeholder="Select ">{{$select.selected.name}}</ui-select-match>
<ui-select-choices repeat="country in ctrl.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>

并在控制器中选择页面加载选项

me.country.selected = {"name":"Albania","code":"AL"};

【讨论】:

  • 这不是我想要的。在您的示例中,您的 ctrl.country.selected 包含整个对象 {"name":"Albania","code":"AL"}。这是我试图避免的。我的 currentItem.PriorityValue 只包含字符串“(2) Normal”
【解决方案2】:

我也遇到了和你一样的问题,所以我也想分享我的解决方案,所以我认为这是你的数据结构是 lst。我了解您仅存储或从数据库中获取值,因此我添加了过滤器以根据值 lst 获取对象( 2) Normal 并将其分配给 option.PriorityValue

  var lst = [
    {"text":"(1) High","value":"(1) High"}, 
    {"text":"(2) Normal","value":"(2) Normal"}, 
    {"text":"(3) Low","value":"(3) Low"} 
  ];
  $scope.data = {
      priority: {
        options: lst
      }
  };
  //You can change the value '(2) Normal'
  var newTemp = $filter("filter")(lst, {"value":'(2) Normal'});
  $scope.option = {
     PriorityValue: newTemp[0]
  }

这是标记,如果您只想在选择下拉列表时获取值,请使用 option.PriorityValue.value

  <ui-select ng-model="option.PriorityValue" theme="selectize" style="width: 300px;" title="Choose a priority">
      <ui-select-match placeholder="Select priority in the list...">{{$select.selected.value}}</ui-select-match>
      <ui-select-choices repeat="option in (data.priority.options |  filter: $select.search) track by option.value">
          <span ng-bind-html="option.text | highlight: $select.search"></span>
          <small ng-bind-html="option.value | highlight: $select.search"></small>
      </ui-select-choices>
  </ui-select>

这是我为你做的:

http://plnkr.co/edit/IIjSaHOHgzE9l2JV3b1Q?p=preview

对不起我的英语。

【讨论】:

  • 感谢您的回复,但这也并没有真正涵盖我正在寻找的内容。您将 ng-model 绑定到 option.PriorityValue,您将其设置为整个对象 {"value":'(2) Normal'}。我不想要对象我只想要字符串“(2) Normal”。
  • 除此之外,我现在可以按预期工作,但有一个问题。加载表单时 (2) 应选择正常。然后,当我单击打开下拉列表时,所有三个选项都按原样列出。然后我输入过滤器,不相关的就消失了,但是当我退格并删除我输入的内容时,只有两个当前未选择的选项会在下拉列表中返回。如何让它停止过滤掉当前选择的选项?当没有输入文本时,我希望显示所有选项,即使是当前选择的选项。
  • 你检查过angular-ui-select的文档吗?我认为您想要发生的事情可能在 angular-ui-select 插件上不可用,我确实查看了 stackoverflow 并阅读了文档,返回将始终是一个对象并且与所选值匹配,它也应该是一个相同的对象结构在列表中,这就是我使用 $filter 来创建该解决方法的原因。
  • 所以我尝试了过滤,我输入了一些文本,退格直到所有内容都被清除并且它完全显示了所有选项,我认为它不会过滤掉当前选择的选项,你能检查一下再次?你的过滤字符串是什么?您可以复制我制作的 plunker 并尝试使用它。
  • 是的,我也不知道发生了什么。我基本上在下面的 plunkr 中复制了我的代码,但一切正常,但由于某种原因,在我的实际解决方案中,一旦您键入过滤当前选定的选项,就会从下拉列表中删除 plnkr.co/edit/losvPD?p=preview
猜你喜欢
  • 2018-05-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-11
  • 2017-05-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多