当前位置: 首页 > 面试题库 >

Angular JS-自动聚焦输入并显示typeahead下拉菜单-ui.bootstrap.typeahead

麻和雅
2023-03-14
问题内容

我正在使用Angular JS-ui.bootstrap.typeahead:

我想单击一个按钮并聚焦于一个输入字段,并自动显示预先输入建议下拉列表。我有一条指令,当单击按钮时,该指令会自动聚焦输入字段。如何自动显示下拉菜单,以便用户可以使用向下箭头或单击来快速选择用户?

我用可编辑的ui-bootstrap JS文件创建了一个Plunker:

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

这是我的完整脚本:

    <!doctype html>
    <html ng-app="plunker">
      <head>
        <link href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" rel="stylesheet">
        <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.2.2/angular.js"></script>
        <script src="ui-bootstrap-tpls-0.10.0.js"></script>
      </head>
      <body>

    <script>
      angular.module('plunker', ['ui.bootstrap'])
      .directive('focusMe', function($timeout, $parse) {
        return {
            //scope: true,   // optionally create a child scope
            link: function(scope, element, attrs) {
                var model = $parse(attrs.focusMe);
                scope.$watch(model, function(value) {
                    if(value === true) { 
                        $timeout(function() {
                            element[0].focus(); 
                        });
                    }
                });

            }
        };
    });
    function TypeaheadCtrl($scope, $http) {

      $scope.selected = undefined;
      $scope.states = ['Alabama', 'Alaska', 'Arizona', 'Arkansas', 'California', 'Colorado', 'Connecticut', 'Delaware', 'Florida', 'Georgia', 'Hawaii', 'Idaho', 'Illinois', 'Indiana', 'Iowa', 'Kansas', 'Kentucky', 'Louisiana', 'Maine', 'Maryland', 'Massachusetts', 'Michigan', 'Minnesota', 'Mississippi', 'Missouri', 'Montana', 'Nebraska', 'Nevada', 'New Hampshire', 'New Jersey', 'New Mexico', 'New York', 'North Dakota', 'North Carolina', 'Ohio', 'Oklahoma', 'Oregon', 'Pennsylvania', 'Rhode Island', 'South Carolina', 'South Dakota', 'Tennessee', 'Texas', 'Utah', 'Vermont', 'Virginia', 'Washington', 'West Virginia', 'Wisconsin', 'Wyoming'];
      $scope.opened = false;

      $scope.open = function() {
        $scope.opened = true;
      }
      $scope.close = function() {
        $scope.opened = false;
      }
    }

    </script>
    <div class='container-fluid' ng-controller="TypeaheadCtrl">

        <h4>How can I open the typeahead dropdown automatically when button is pressed?</h4>
        <p>I have a directive that automatically focuses on the field but I can't seem to automatically show the typeahead. Even adding down arrow key click support would be great.

        <br/><br/>

        <button class="btn btn-default" ng-show="!opened" ng-click="open()">Open Input and show typeahead!</button>
        <button class="btn btn-default" ng-show="opened" ng-click="close()">Close Input</button>
        <br/><br/>

        <input type="text"
        focus-me="opened"
        ng-show="opened"
        ng-model="selected" 
        typeahead="state for state in states | filter:$viewValue | limitTo:8" 
        class="form-control">


        <br/>
        <pre ng-show="opened">Model: {{selected | json}}</pre>


    </div>
      </body>
    </html>

问题答案:

正如HarishR在评论中提到的那样,尚无对此功能的内置支持。

但是我只想尝试一下,结果是这样:http :
//plnkr.co/edit/Qrnat8yTvISuM1qHHDlA?p=preview

它包含许多使它起作用的技巧:

  1. 为了使用.trigger()包含jQuery,可以用本机JS替换,但我很懒。
  2. 使用ng-focus调用.trigger(’input’)触发字体弹出
  3. 使用ng-trim =“ false”禁用输入的值自动修整
  4. 一个自定义的empty-typeahead指令,可与ngModel的控制器进行交互,以应用secretEmptyKey逻辑来绕过typeahead-min-length检查:
        .directive('emptyTypeahead', function () {
      return {
        require: 'ngModel',
        link: function (scope, element, attrs, modelCtrl) {
          // this parser run before typeahead's parser
          modelCtrl.$parsers.unshift(function (inputValue) {
            var value = (inputValue ? inputValue : secretEmptyKey); // replace empty string with secretEmptyKey to bypass typeahead-min-length check
            modelCtrl.$viewValue = value; // this $viewValue must match the inputValue pass to typehead directive
            return value;
          });

          // this parser run after typeahead's parser
          modelCtrl.$parsers.push(function (inputValue) {
            return inputValue === secretEmptyKey ? '' : inputValue; // set the secretEmptyKey back to empty string
          });
        }
      }
    })
  1. 一个自定义过滤器比较器函数,当一个参数为secretEmptyKey时始终返回true(显示所有结果):

    $scope.stateComparator = function (state, viewValue) {
    

    return viewValue === secretEmptyKey || (‘’+state).toLowerCase().indexOf((‘’+viewValue).toLowerCase()) > -1;
    };

  2. 删除limitTo过滤器以显示所有结果

  3. 如果内容太长,则设置max-height和溢出CSS属性以显示滚动条

做完了!



 类似资料:
  • 这在当前版本的AngularJS中存在吗?我注意到代码中有一个BOOLEAN_ATTR,它获得AngularJS支持的所有attr。我不想修改它,因为害怕改变版本和忘记更新。

  • 问题内容: 我有以下形式的数据 我想要两个下拉菜单。第一个下拉列表将显示“名称”。当用户从第一个下拉列表中选择名称时,应在第二个下拉列表中填充相应的“版本”。 无效的jsfiddle链接:http://jsfiddle.net/fynVy/174/ 问题答案: 您需要调整HTML模板,以便第一个下拉列表显示服务器名称,并且第二个下拉列表的选项基于所选下拉列表(第一个下拉列表的ngModel)中的版

  • 问题内容: 我有输入要显示格式化的数字。通常,当它没有焦点时,应该显示一个格式化的字符串,例如’$ 50,000.00’。但是当它具有焦点时,它应该显示原始值,例如用于编辑的50000。 有内置功能吗?谢谢! 问题答案: 这是一条指令(),它可以执行您想要的操作。 请注意,只有元素的显示值才被格式化(模型值将始终为未格式化)。 这个想法是您为和事件注册侦听器,并根据元素的焦点状态更新显示值。 另请

  • 我有一个项目,我使用日历视图,内置的日历为Android。 因此,我在完成本日历的某些特定任务时面临一些困难。我真的希望我能在StackOverflow上找到解决方案。 > 如果没有,那就停止阅读。 这就是我的问题: 我想显示一个下拉菜单从选定的一天,与一些元素,用户可以选择。如果用户选择某个元素,则下拉菜单的状态将仅在选定日期更改,即如果用户单击13/1,下拉菜单应显示10个元素。如果用户选择一

  • 实现带动画效果的下拉菜单。用户按下菜单按钮,出现下拉按钮,用户松开菜单按钮,下拉按钮收回。 [Code4App.com]

  • 问题内容: 首先,我讨厌提出一个已经解决的问题,但是您应该知道我在此站点上找到的其他选项对我不起作用。 基本上,我想构造一个简短的表格,其中有两个下拉框。始终显示第一个,默认情况下隐藏第二个。在第一个下拉框中选择某个选项后,我希望第二个下拉框显示。这是我的意思的完美示例: 但是,与上面的示例不同,我有单独的一组选项要显示在下拉框中,以显示在第一个下拉框中选择的每个选项,而不仅是其中一个选项。换句话