angular - 模板解析错误: Can't bind to 'ngbTypeahead' since it isn't a known property of 'input'

标签 angular ng-bootstrap typeahead

当我运行我的 Angular 项目时,出现错误“模板解析错误:无法绑定(bind)到‘ngbTypeahead’,因为它不是‘input’的已知属性”。到处寻找但找不到解决方案。

这是我的 html。

<div class="kt-section__content">
    <label for="typeahead-basic">Search for a state:</label>
    <input id="typeahead-basic" type="text" class="form-control" [(ngModel)]="model" [ngbTypeahead]="search" />
</div>

这是组件.ts

import { Component, OnInit } from '@angular/core';
import { Observable } from 'rxjs';
import { debounceTime, distinctUntilChanged, map } from 'rxjs/operators';

const states = ['Alabama', 'Alaska', 'American Samoa', 'Arizona', 'Arkansas', 'California', 'Colorado',
  'Connecticut', 'Delaware', 'District Of Columbia', 'Federated States Of Micronesia', 'Florida', 'Georgia',
  'Guam', 'Hawaii', 'Idaho', 'Illinois', 'Indiana', 'Iowa', 'Kansas', 'Kentucky', 'Louisiana', 'Maine',
  'Marshall Islands', 'Maryland', 'Massachusetts', 'Michigan', 'Minnesota', 'Mississippi', 'Missouri', 'Montana',
  'Nebraska', 'Nevada', 'New Hampshire', 'New Jersey', 'New Mexico', 'New York', 'North Carolina', 'North Dakota',
  'Northern Mariana Islands', 'Ohio', 'Oklahoma', 'Oregon', 'Palau', 'Pennsylvania', 'Puerto Rico', 'Rhode Island',
  'South Carolina', 'South Dakota', 'Tennessee', 'Texas', 'Utah', 'Vermont', 'Virgin Islands', 'Virginia',
  'Washington', 'West Virginia', 'Wisconsin', 'Wyoming'];

@Component({
  selector: 'kt-grn',
  templateUrl: './grn.component.html',
  styleUrls: ['./grn.component.scss']
})
export class GrnComponent implements OnInit {
  public model: any;

  search = (text$: Observable<string>) =>
    text$.pipe(
      debounceTime(200),
      distinctUntilChanged(),
      map(term => term.length < 2 ? []
        : states.filter(v => v.toLowerCase().indexOf(term.toLowerCase()) > -1).slice(0, 10))
    )

  constructor() { }

  ngOnInit() {
  }

}

这是来自 app.module.ts 的代码

import { NgbModule } from '@ng-bootstrap/ng-bootstrap';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';

@NgModule({
    declarations: [AppComponent],
    imports: [
        BrowserAnimationsModule,
        BrowserModule,
        FormsModule,
        ReactiveFormsModule,
        NgbModule,
    ],
    exports: [],
    providers: [],
    bootstrap: [AppComponent]
})
export class AppModule {
}

有人可以帮助我吗?

最佳答案

您还需要导入 NgbTypeaheadModule。

import { NgbModule, NgbTypeaheadModule } from '@ng-bootstrap/ng-bootstrap';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';

@NgModule({
    declarations: [AppComponent],
    imports: [
        BrowserAnimationsModule,
        BrowserModule,
        FormsModule,
        ReactiveFormsModule,
        NgbModule,
        NgbTypeaheadModule
    ],
    exports: [],
    providers: [],
    bootstrap: [AppComponent]
})
export class AppModule {
}

编辑:这更多地与库要求您使用他们的库的方式有关,而不是纯粹的 Angular 的东西。有些库会在一个模块中为您提供所有内容。 UI 组件模块(例如 Material 和 NgbBootstrap)允许您单独导入组件模块,因此您可以将导入的 javascript 的大小保持在最小。

关于angular - 模板解析错误: Can't bind to 'ngbTypeahead' since it isn't a known property of 'input' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60229302/

相关文章:

angular - 将 Material2 属性添加到 WebStorm

具有所选属性的 Angular 选择选项不起作用

Angular 4从模态获取表单值

Angular : Showing no search result in typeahead when no match is found for entered string

jquery - Typeahead、Bloodhound 和选择数据源问题

Angular 6 Ngx typeahead 选项渲染

javascript - 通过发出 http 请求来获取路由解析器的数据,以便第二个请求的结果取决于第一个请求的结果

json - *ngfor json 数组对象中的错误显示

css - 替代/深/

angular - 如何使用 Angular 2/Bootstrap 4 为 ng-bootstrap 控件自定义 CSS