简体   繁体   English

ngx-bootstrap typeahead 不显示下拉菜单

[英]ngx-bootstrap typeahead not showing dropdown

I'm migrating an application from AngularJS to Angular and I've hit a brick wall with the new implementation for typeahead, it's been a day now since and I've tried with several API, finally decided to go for the most similar one to what I was using in AngularJS version ( this )我正在将一个应用程序从 AngularJS 迁移到 Angular 并且我已经用新的 typeahead 实现碰到了砖墙,从那以后已经过了一天,我已经尝试了几个 API,最后决定去寻找最相似的一个我在 AngularJS 版本中使用的是什么(这个

So, in my template I do this: (you can also find pluker link at the bottom)所以,在我的模板中,我这样做:(你也可以在底部找到 pluker 链接)

<input [(ngModel)]="publication"
       [typeahead]="publications"
       typeaheadOptionField="name"
       typeaheadOptionsLimit="25"
       placeholder="Type it"
       class="form-control">

Against this component.ts:针对这个 component.ts:

  import { Component, OnInit, OnDestroy } from '@angular/core';
import { ActivatedRoute, Router } from '@angular/router';
import { Subscription } from 'rxjs/Rx';
import { JhiEventManager, JhiParseLinks, JhiPaginationUtil, JhiAlertService } from 'ng-jhipster';
import { FormService } from './form.service';
import { ITEMS_PER_PAGE, Principal, ResponseWrapper } from '../shared';
import { PaginationConfig } from '../blocks/config/uib-pagination.config';
import {FormDTO} from './formDTO.model';


@Component({
    selector: 'jhi-form',
    templateUrl: './uploadData.html'
})

export class FormComponent implements OnInit, OnDestroy {

    publication: String;
    public publications: any[] = [ {id: 1, name: 'this'}, {id: 2, name: 'is'}, {id: 21, name: 'list'}, {id: 4, name: 'of'}, {id: 5, name: 'string'}, {id: 6, name: 'element'}]

  /* in my app I hold a number of variables, cutted them out as irrelevant to issue */

    constructor(
        private alertService: JhiAlertService,
        private formService: FormService,
        private eventManager: JhiEventManager
/*here I put in a number of entities that query the backend, cutted them out for this example as they're irrelevant*/
    ) {
    }

    ngOnInit() {
        this.loadAll();
        this.registerChangeInForm();
    }

    ngOnDestroy() {
        this.eventManager.destroy(this.eventSubscriber);
    }

    loadAll() {
        this.isSaving = false;
        /* loads a number of list for entities, irrelevant code for the question */
    }

    save() {
        console.log('save');
    }



    search(id) {
        this.formService.find(id).subscribe(
            (result) => (this.formDTO = result, this.formDTOLoaded = true),
            (error) => (alert('Publication with Id:' + id + ' was not found in Database'), this.formDTOLoaded = false));
        this.loadAll();

    }

    registerChangeInForm() {
        this.eventSubscriber = this.eventManager.subscribe('formListModification', (response) => this.loadAll());
    }

    trackPublicationTypeById(index: number, item: PublicationType) {
        return item.id;
    }

    private onError(error) {
        this.alertService.error(error.message, null, null);
    }

}

And the module.ts:和模块.ts:

  import { NgModule, CUSTOM_ELEMENTS_SCHEMA } from '@angular/core';
import { RouterModule } from '@angular/router';

import { DrugQualityDataManagerSharedModule } from '../shared';
import {formRoute} from './form.route';
import {FormComponent} from './form.component';
import {FormService} from './form.service';
import { NguiAutoCompleteModule } from '@ngui/auto-complete';
import { TypeaheadModule } from '../../../../../node_modules/ngx-bootstrap/typeahead';
import { FormsModule } from '@angular/forms';

 const ENTITY_STATES = [
     ...formRoute,
 ];

@NgModule({
    imports: [
        DrugQualityDataManagerSharedModule,
        FormsModule,
        TypeaheadModule.forRoot(),
        RouterModule.forRoot(ENTITY_STATES, { useHash: true })
    ],
    declarations: [
        FormComponent,
    ],
    entryComponents: [
        FormComponent,
    ],
    providers: [
        FormService,
     ],
    schemas: [CUSTOM_ELEMENTS_SCHEMA]
})
export class DrugQualityDataManagerFormModule {}

The array of publications is a simplified one, I created it to test the typeahead, which should just display the list of objects that have the name property most similar to what's being typed in the input box, but it does absolutely nothing.出版物数组是一个简化的数组,我创建它是为了测试预先输入,它应该只显示名称属性与输入框中键入的内容最相似的对象列表,但它绝对没有任何作用。

When debugging on the Web Developer tools I can see the ng-blur options triggering when I type in the box, but nothing happens from there在 Web Developer 工具上调试时,我可以看到在框中键入时触发的 ng-blur 选项,但没有任何反应

The console is clean of errors and the placeholder displays fine until I start typing, which is the expected behaviour控制台没有错误,占位符显示正常,直到我开始输入,这是预期的行为

I would like someone to help me solve this puzzle, but it would be really awesome if someone could also point out how to debug an issue like this?我希望有人能帮我解决这个难题,但如果有人也能指出如何调试这样的问题,那真是太棒了?

EDIT: to add plunker编辑:添加plunker

Make sure that your versions of Angular and that of Bootstrap are compatible.确保您的 Angular 版本和 Bootstrap 版本兼容。 It happened when I was using Angular 4 with ng2-bootstrap 1.6.x.它发生在我将 Angular 4 与 ng2-bootstrap 1.6.x 一起使用时。 Better yet, instead of ng2-bootstrap you should be using ngx-bootstrap.更好的是,您应该使用 ngx-bootstrap 而不是 ng2-bootstrap。 To get the dropdown working, add the container attribute:要使下拉菜单正常工作,请添加容器属性:

<div class="btn-group" dropdown  container="body"></div>

First of all首先

import { TypeaheadModule } from '../../../../../node_modules/ngx-bootstrap/typeahead';

Should be:应该是:

import { TypeaheadModule } from 'ngx-bootstrap/typeahead';

Without binding [] , options limit will be set as a string typeaheadOptionsLimit="25" , so it should be [typeaheadOptionsLimit]="25"没有绑定[] ,选项限制将被设置为字符串typeaheadOptionsLimit="25" ,所以它应该是[typeaheadOptionsLimit]="25"

I don't even see that typeahead was attached :( remove this: schemas: [CUSTOM_ELEMENTS_SCHEMA] and most probably you will see an error that typeahead is unknown property我甚至不看到预输入附着:(删除此: schemas: [CUSTOM_ELEMENTS_SCHEMA]和最有可能你会看到一个错误, typeahead未知属性

Then try to remove all options and apply them from samples:然后尝试删除所有选项并从示例中应用它们:

https://valor-software.com/ngx-bootstrap/#/typeahead#static https://valor-software.com/ngx-bootstrap/#/typeahead#static

You should end up with something like this:你应该得到这样的结果:

https://valor-software.com/ngx-bootstrap/#/typeahead#async https://valor-software.com/ngx-bootstrap/#/typeahead#async

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM