【问题标题】:Why is the ui-select not clearing on selection every 2nd time?为什么 ui-select 不会每 2 次清除一次选择?
【发布时间】:2014-12-10 16:08:36
【问题描述】:

ui-select 中,当我选择一个选项时,on-select 回调确实会触发,但该选项不会每两次清除一次。

ui-select 有两种状态:

  1. 按钮 - 当我没有实际点击它时
  2. 输入 - 当我点击它并让我搜索时

当它启动时,按钮有我的占位符文本“搜索...”。我点击它,开始搜索,它会弹出一个列表,我选择一个选项,on-select 触发,其中我清除了ng-model

然而,我每进行一次选择,它就会忽略我回调中的 ng-model 清除。

<ui-select ng-model="result.selected" theme="bootstrap" class="search" on-select="searchselect($item)" reset-search-input="true" on-select="selected($item)">
    <ui-select-match placeholder="Search...">{{$select.selected.name}}</ui-select-match>
    <ui-select-choices refresh="search({val:$select.search})" refresh-delay="300" repeat="item in results">
        <div ng-bind="item.name"></div>
    </ui-select-choices>
</ui-select>

这是我的on-select 代码:

  $scope.selected = function(item) {
     $scope.picked = item;
     $scope.result.selected = "";
  };

我修改了基本的 ui-select 示例 plunkr 以复制它:http://plnkr.co/edit/16DRYH8MbPJOy0GpogZz?p=preview

  1. 点击选择
  2. 搜索文字,我发现“123”最简单
  3. 选择一个选项
  4. 看到ng-model被清除了
  5. 再次搜索
  6. 选择另一个选项,或相同的选项
  7. 看到ng-model没有被清除

这是图片。

首字母:

在搜索过程中:

选择后(每第二个) - 这是我的问题

为什么在每2次选择后按钮最后一个图像中没有清除它?

【问题讨论】:

    标签: angularjs angular-ui ui-select


    【解决方案1】:

    晚了,但也许有帮助:我猜这与 ui-select 中 angular-digest 循环的错误实现有关。 See scope lifecycle of the angular documentation.

    那么,你能做些什么呢:

    • 将您对选定回调所做的所有修改都包装在 $timeout 中,因为 Angular 会自动将其中的所有内容包装在一个安全的摘要循环中。 示例:

      $scope.selected = function(item) {
        $timeout(function(){
          $scope.picked = item;
          $scope.result.selected = "";
        }); 
      };
      
    • 使用 $watch 表达式代替回调函数:

      $scope.$watch('result.selected', function(newVal){
        if(newVal !== ''){
          $scope.picked = newVal;
          $scope.result.selected = '';
        }
      }); 
      

    我猜后者效率更高,因为它不会触发完整的摘要循环。 (但我不是角度内部的专家。)

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-16
    • 1970-01-01
    • 2010-10-13
    • 2022-11-10
    • 2021-07-14
    相关资源
    最近更新 更多