GVKun编程网logo

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

4

本文将带您了解关于AngularJS-自动聚焦输入并显示typeahead下拉菜单-ui.bootstrap.typeahead的新内容,同时我们还将为您解释angularjs下拉框联动的相关知识,另

本文将带您了解关于Angular JS-自动聚焦输入并显示typeahead下拉菜单-ui.bootstrap.typeahead的新内容,同时我们还将为您解释angularjs下拉框联动的相关知识,另外,我们还将为您提供关于angular – ng-bootstrap typeahead TypeError:Object(…)不是函数、angular – 如何在Reactive Form上使用ng-bootstrap Typeahead组件、angular-ui-bootstrap – 如何将Angular UI Typeahead对齐到右侧、Angularjs bootstrap typeahead无效:错误:[filter:notarray]的实用信息。

本文目录一览:

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

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

我正在使用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>
    <divng-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/>

        <buttonng-show="!opened" ng-click="open()">Open Input and show typeahead!</button>
        <buttonng-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">


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


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

angular – ng-bootstrap typeahead TypeError:Object(…)不是函数

angular – ng-bootstrap typeahead TypeError:Object(…)不是函数

我想在angular5应用程序中使用角度bootstrap typeahead linkToTypeahead.
但我不断收到以下错误:

ERROR TypeError: Object(...) is not a function
at new NgbTypeahead (typeahead.js:52)
at createClass (core.js:12449)
at createDirectiveInstance (core.js:12284)
at createViewNodes (core.js:13742)
at callViewAction (core.js:14176)
at execComponentViewsAction (core.js:14085)
at createViewNodes (core.js:13770)
at callViewAction (core.js:14176)
at execComponentViewsAction (core.js:14085)
at createViewNodes (core.js:13770)

并且:

ERROR TypeError: Cannot read property 'instance' of undefined
at nodeValue (core.js:11753)
at Object.eval [as handleEvent] (NgbdTypeaheadBasic.html:9)
at handleEvent (core.js:13547)
at callWithDebugContext (core.js:15056)
at Object.debugHandleEvent [as handleEvent] (core.js:14643)
at dispatchEvent (core.js:9962)
at eval (core.js:10587)
at HTMLDocument.eval (platform-browser.js:2628)
at ZoneDelegate.invokeTask (zone.js:421)
at Object.onInvokeTask (core.js:4740)

我从网站link复制了Simple Typeahead代码.

我的package.json文件:

{
  “名称”:“测试型提前”,
  “版本”:“0.0.0”,
  “执照”:“麻省理工学院”,
  “脚本”:{
    “ng”:“ng”,
    “开始”:“ng serve”,
    “build”:“ng build –prod”,
    “测试”:“ng测试”,
    “lint”:“ng lint”,
    “e2e”:“ng e2e”
  },
  “私人”:是的,
  “依赖”:{
    “@ angular / animations”:“^ 5.2.0”,
    “@ angular / common”:“^ 5.2.0”,
    “@ angular / compiler”:“^ 5.2.0”,
    “@ angular / core”:“^ 5.2.0”,
    “@ angular / forms”:“^ 5.2.0”,
    “@ angular / http”:“^ 5.2.0”,
    “@ angular / platform-b​​rowser”:“^ 5.2.0”,
    “@ angular / platform-b​​rowser-dynamic”:“^ 5.2.0”,
    “@ angular / router”:“^ 5.2.0”,
    “@ ng-bootstrap / ng-bootstrap”:“^ 2.0.0-alpha.0”,
    “bootstrap”:“^ 4.1.0”,
    “core-js”:“^ 2.4.1”,
    “rxjs”:“^ 5.5.6”,
    “zone.js”:“^ 0.8.19”
  },
  “devDependencies”:{
    “@ angular / cli”:“~1.7.3”,
    “@ angular / compiler-cli”:“^ 5.2.0”,
    “@ angular / language-service”:“^ 5.2.0”,
    “@ types / jasmine”:“~2.8.3”,
    “@ types / jasminewd2”:“~2.0.2”,
    “@ types / node”:“~6.0.60”,
    “codelyzer”:“^ 4.0.1”,
    “茉莉核心”:“~2.8.0”,
    “jasmine-spec-reporter”:“~4.2.1”,
    “业力”:“~2.0.0”,
    “karma-chrome-launcher”:“~2.2.0”,
    “karma-coverage-istanbul-reporter”:“^ 1.2.1”,
    “karma-jasmine”:“~1.1.0”,
    “karma-jasmine-html-reporter”:“^ 0.2.2”,
    “量角器”:“~5.1.2”,
    “ts-node”:“~4.1.0”,
    “tslint”:“~5.9.1”,
    “打字稿”:“~2.5.3”
  }
}

到目前为止我尝试了什么:

>用npm重新安装node_modules – 没有帮助
>使用角度CLI开始了一个全新的项目并在那里试用了代码 – 没有帮助.

任何帮助是极大的赞赏!

解决方法

所以我的问题通过安装稳定版本的ng-bootstrap解决了:

所以package.json文件:

`dependencies
 ....
 "@ng-bootstrap/ng-bootstrap": "^1.1.1",....
 dependencies`

感谢JB Nizet和Niladri的快速解答

编辑:另外,正如@Niladri在评论中所说,你需要为你想要使用的模块使用forRoot().这是针对Angular 5的official documentation of version 1.1,这里说的是,但更新版本的角度曲线的新文档并未说明,因为它已被更改.所以记住这一点!

angular – 如何在Reactive Form上使用ng-bootstrap Typeahead组件

angular – 如何在Reactive Form上使用ng-bootstrap Typeahead组件

我正在尝试在带有Angular 2的Reactive Form上使用ng-bootstrap Typeahead组件,在查看 Typeahead’s documentation上的示例代码后,我无法使其工作.

目前,我有类似的东西:

<input type="text" aria-describedby="cityValid"id="inputCity" [formControl]="userForm.controls.city" [ngbTypeahead]="search">

使用与示例代码几乎相同的搜索功能,但它会因以下堆栈跟踪而失败

error_handler.js:53 TypeError: Cannot read property 'subscribe' of undefined
at NgbTypeahead._subscribetoUserInput (typeahead.js:158)
at NgbTypeahead.ngOnInit (typeahead.js:52)
at DebugAppView._View_UserFormComponent0.detectChangesInternal (UserFormComponent.ngfactory.js:1093)
at DebugAppView.AppView.detectChanges (view.js:271)
at DebugAppView.detectChanges (view.js:376)
at DebugAppView.AppView.detectViewChildrenChanges (view.js:297)
at DebugAppView._View_ExecutionFormComponent3.detectChangesInternal (ExecutionFormComponent.ngfactory.js:551)
at DebugAppView.AppView.detectChanges (view.js:271)
at DebugAppView.detectChanges (view.js:376)
at DebugAppView.AppView.detectContentChildrenChanges (view.js:289)

我究竟做错了什么?如果有一个关于如何在Reactive Forms上使用ng-bootstrap组件的一般指南会很棒,因为该示例基于基于模板的示例.

谢谢!

解决方法

没关系,问题是我采用了示例中出现的搜索功能并将其转换为普通函数但没有返回任何值.

我有

searchCity (text$: Observable<string>) {
  text$
  .debounceTime(200)
  .distinctUntilChanged()
  .map(term => term.length < 2 ? []
    : cities.filter(v => new RegExp(term,'gi').test(v)).splice(0,10));
}

在text $fixed之前添加一个return语句.

angular-ui-bootstrap – 如何将Angular UI Typeahead对齐到右侧

angular-ui-bootstrap – 如何将Angular UI Typeahead对齐到右侧

我在RTL语言网站(阿拉伯语)中使用angular-ui-bootstrap.默认情况下,Typeahead将所有结果对齐到左侧.如何改变它?

这在angular-ui-bootstrap网站中提到:

The $uibPosition service provides a set of DOM utilities used internally to absolute-position an element in relation to another element (tooltips,popovers,typeaheads etc…).

我无法理解.有什么例子吗?

解决方法

这对我有用:

.rtl .dropdown-menu{
     float:right;
     position:inherit;
  }

Angularjs bootstrap typeahead无效:错误:[filter:notarray]

Angularjs bootstrap typeahead无效:错误:[filter:notarray]

我需要使用typeahead进行http调用来获取建议选项

$scope.getlist = function getlistofNames(name) {
 return $http({
   method: 'GET',url: '/v1/'+name,data: ""
 }).then(function(response){

     if(response.data.statusMessage !== 'SUCCESS'){
         response.errorMessage = "Error while processing request. Please retry."
           return response;
     }

         else {
           return response.data.payload;
         }

       },function(response){
       response.errorMessage = "Error while processing request"
           return response;
       }
       );
}

response.data.payload是一个对象数组,它已成功获取但我收到此错误
错误:[filter:notarray] http://errors.angularjs.org/1.4.5/filter/notarray?

注意:我使用的是角度1.4.5和Bootstrap v3.1.1

解决方法

我猜你的typeahead标记看起来像这样:

<input [...] typeahead="item in getItems($viewValue) | filter: $viewValue">

为什么它不起作用:

异步提取项目数组时会发生此问题.在你的情况下,getItems函数被称为getlistofNames,由于调用了$http,你的项目确实是异步获取的.因此,在发生错误时,getlistofNames()仍然是未解析的promise对象,而不是名称数组.

你怎么能做这个工作:

从模板中删除过滤器.您应该在getItems中返回之前过滤该数组.理想情况下,您希望在服务器端进行过滤.实际上,服务器接收用户键入的子字符串(这是$viewValue参数),因此它具有过滤数组的所有数据.这样可以防止返回所有元素并缩短响应时间.
或者,您可以在承诺的回调中过滤客户端:

$scope.getList = function getlistofNames(name) {
    return $http(...}).then(
        function(response){
            // filter response.data.payload according to 
            // the 'name' ($viewValue) substring entered by the user
            return filteredArray; // <- no need to pipe a filter in the template anymore
        }
    );
};

今天关于Angular JS-自动聚焦输入并显示typeahead下拉菜单-ui.bootstrap.typeaheadangularjs下拉框联动的介绍到此结束,谢谢您的阅读,有关angular – ng-bootstrap typeahead TypeError:Object(…)不是函数、angular – 如何在Reactive Form上使用ng-bootstrap Typeahead组件、angular-ui-bootstrap – 如何将Angular UI Typeahead对齐到右侧、Angularjs bootstrap typeahead无效:错误:[filter:notarray]等更多相关知识的信息可以在本站进行查询。

本文标签: