[英]Template parse errors: Can't bind to 'ngbTypeahead' since it isn't a known property of 'input
Does not work ngbTypeahead . 不起作用ngbTypeahead 。 The console returns the following message:
控制台返回以下消息:
Error: Template parse errors:
Can't bind to 'ngbTypeahead' since it isn't a known property of 'input'. (" <input id="typeahead-basic" type="text" class="form-control" [(ngModel)]="model" [ERROR ->][ngbTypeahead]="search"/>
<hr>
"): ng:///ProductModule/UserProductsComponent.html@86:100
at syntaxError (compiler.js:485)
at TemplateParser.webpackJsonp../node_modules/@angular/compiler/esm5/compiler.js.TemplateParser.parse (compiler.js:24668)
at JitCompiler.webpackJsonp../node_modules/@angular/compiler/esm5/compiler.js.JitCompiler._parseTemplate (compiler.js:34621)
at JitCompiler.webpackJsonp../node_modules/@angular/compiler/esm5/compiler.js.JitCompiler._compileTemplate (compiler.js:34596)
at compiler.js:34497
at Set.forEach (<anonymous>)
at JitCompiler.webpackJsonp../node_modules/@angular/compiler/esm5/compiler.js.JitCompiler._compileComponents (compiler.js:34497)
at compiler.js:34367
at Object.then (compiler.js:474)
at JitCompiler.webpackJsonp../node_modules/@angular/compiler/esm5/compiler.js.JitCompiler._compileModuleAndComponents (compiler.js:34366)
file user-products.component.html in which it is placed: file文件user-products.component.html :
<div class="jumbotron">
<input id="typeahead-basic" type="text" class="form-control" [(ngModel)]="model" [ngbTypeahead]="search"/>
</div>
app.module.ts: app.module.ts:
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
import { RouterModule, Routes} from '@angular/router';
import { AppComponent } from './app.component';
import { UserService } from './user.service';
import { ProductModule } from './product/product.module';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { NgbModule } from '@ng-bootstrap/ng-bootstrap';
// ...
@NgModule({
declarations: [AppComponent],
imports: [
BrowserModule,
FormsModule,
NgbModule.forRoot(),
ReactiveFormsModule,
BrowserAnimationsModule,
ProductModule,
RouterModule.forRoot(routes )
// ...
],
bootstrap: [AppComponent]
})
export class AppModule { }
class user-products.component.ts in which the method is search: class user-products.component.ts ,其中搜索方法:
import { User, UserType } from '../../model';
import { Component, OnInit, Input, ViewChild } from '@angular/core';
import { FormsModule, FormGroup, FormBuilder, Validators, FormControl } from '@angular/forms';
import { Router, ActivatedRoute } from '@angular/router';
import { AuthService } from '../../auth.service';
import { Product, ProductType } from './module-product';
import { Observable } from 'rxjs/';
import { NgbTypeahead } from '@ng-bootstrap/ng-bootstrap';
import {Subject} from 'rxjs/Subject';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/merge';
import 'rxjs/add/operator/filter';
import 'rxjs/add/operator/debounceTime';
import 'rxjs/add/operator/distinctUntilChanged';
// ...
const states = ['Alabama', 'Alaska', 'American Samoa', 'Arizona', 'Arkansas', 'California', 'Colorado',
'South Carolina', 'South Dakota', 'Tennessee', 'Texas', 'Utah', 'Vermont', 'Virgin Islands'];
@Component({
selector: 'app-user-products',
templateUrl: './user-products.component.html',
styleUrls: ['./user-products.component.css']
})
export class UserProductsComponent implements OnInit {
@Input()
product: Product;
user: User;
UserType = UserType;
product_name: string;
product_type: ProductType;
ProductType = ProductType;
product_desc: string;
produktForm: FormGroup;
products: Product;
products_: any;
users$: Observable<User[]>;
public model: any;
constructor(private fb: FormBuilder, private route: ActivatedRoute, private router: Router ) { }
search = (text$: Observable<string>) => text$
.debounceTime(200)
.distinctUntilChanged()
.map(term => term.length < 2 ? []
: states.filter(v => v.toLowerCase().indexOf(term.toLowerCase()) > -1).slice(0, 10))
ngOnInit() {
this.produktForm = this.fb.group({
product_name: this.product_name,
product_type: this.product_type,
product_desc: this.product_desc
});
}
// ...
}
version: @ng-bootstrap/ng-bootstrap@1.1.0 版本:@ ng-bootstrap / ng-bootstrap @ 1.1.0
I do not know how to correct the error. 我不知道如何纠正错误。
file .angular-cli.json : 文件.angular-cli.json :
{
"$schema": "./node_modules/@angular/cli/lib/config/schema.json",
"project": {
"name": "test-panel"
},
"apps": [
{
"root": "src",
"outDir": "dist",
"assets": [
"assets",
"favicon.ico"
],
"index": "index.html",
"main": "main.ts",
"polyfills": "polyfills.ts",
"test": "test.ts",
"tsconfig": "tsconfig.app.json",
"testTsconfig": "tsconfig.spec.json",
"prefix": "app",
"styles": [
"../node_modules/bootstrap/dist/css/bootstrap.min.css",
"../node_modules/ngx-toastr/toastr.css",
"styles.css"
],
"scripts": [
"../node_modules/jquery/dist/jquery.min.js",
"../node_modules/bootstrap/dist/js/bootstrap.min.js"
],
"environmentSource": "environments/environment.ts",
"environments": {
"dev": "environments/environment.ts",
"prod": "environments/environment.prod.ts"
}
}
],
"e2e": {
"protractor": {
"config": "./protractor.conf.js"
}
},
"lint": [
{
"project": "src/tsconfig.app.json",
"exclude": "**/node_modules/**"
},
{
"project": "src/tsconfig.spec.json",
"exclude": "**/node_modules/**"
},
{
"project": "e2e/tsconfig.e2e.json",
"exclude": "**/node_modules/**"
}
],
"test": {
"karma": {
"config": "./karma.conf.js"
}
},
"defaults": {
"styleExt": "css",
"component": {}
}
}
@Niladri I updated the code, but the error still occurs. @Niladri我更新了代码,但错误仍然存在。
The file .angular-cli.json
only contains a different configuration for libraries (but the bootstrap files are there for me), but everything else is the same as in @Niladri. 文件
.angular-cli.json
只包含一个不同的库配置(但是引导程序文件适用于我),但其他所有内容都与@Niladri相同。
You are using a wrong import in your app.module . 您在app.module中使用了错误的导入。 You are currently importing from
ngx-bootstrap/typeahead
which is a different library for bootstrap 3. 您当前正在从
ngx-bootstrap/typeahead
导入,这是bootstrap 3的不同库。
But according to the documentation you should be using ng-bootstrap
package not from ngx-bootstrap
to use the directive [ngbTypeahead]
. 但根据文档,您应该使用
ng-bootstrap
包而不是ngx-bootstrap
来使用指令[ngbTypeahead]
。 So you need to import the below NgbModule
module like below code in app.module 所以你需要在
NgbModule
导入下面的NgbModule
模块,如下面的代码
import { NgbModule } from '@ng-bootstrap/ng-bootstrap';
@NgModule({
imports : [
//...
NgbModule.forRoot(),
],
//...
})
export class AppModule {}
Now you can using typeahead directive in your component like below 现在您可以在组件中使用typeahead指令,如下所示
<input id="typeahead-focus" type="text" class="form-control" [(ngModel)]="user" [ngbTypeahead]="search" (focus)="focus$.next($event.target.value)" (click)="click$.next($event.target.value)" />
Working demo 工作演示
https://stackblitz.com/angular/rdkongmjovr?file=app%2Ftypeahead-basic.html https://stackblitz.com/angular/rdkongmjovr?file=app%2Ftypeahead-basic.html
Official documentation 官方文件
https://ng-bootstrap.github.io/#/components/typeahead/examples https://ng-bootstrap.github.io/#/components/typeahead/examples
Edit : Here is the complete code which is working for me . 编辑:这是完整的代码,对我有用。 For simplicity I have commented out few service calls using AuthService .
为简单起见,我使用AuthService注释了一些服务调用。
.angular-cli.json .angular-cli.json
"apps": [
{
"root": "src",
"outDir": "dist",
"assets": [
"assets",
"favicon.ico"
],
"index": "index.html",
"main": "main.ts",
"polyfills": "polyfills.ts",
"test": "test.ts",
"tsconfig": "tsconfig.app.json",
"testTsconfig": "tsconfig.spec.json",
"prefix": "app",
"styles": [
"./app/bootstrap.min.css",
"styles.css"
],
"scripts": [],
"environmentSource": "environments/environment.ts",
"environments": {
"dev": "environments/environment.ts",
"prod": "environments/environment.prod.ts"
}
}
],
package.json 的package.json
"dependencies": {
"@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-browser": "^5.2.0",
"@angular/platform-browser-dynamic": "^5.2.0",
"@angular/router": "^5.2.0",
"@ng-bootstrap/ng-bootstrap": "^1.0.4",
"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",
"jasmine-core": "~2.8.0",
"jasmine-spec-reporter": "~4.2.1",
"karma": "~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",
"protractor": "~5.1.2",
"ts-node": "~4.1.0",
"tslint": "~5.9.1",
"typescript": "~2.5.3"
}
typeaheaddemo.component.ts typeaheaddemo.component.ts
import {NgbTypeahead} from '@ng-bootstrap/ng-bootstrap';
import {Subject} from 'rxjs/Subject';
import { Component,ViewChild,OnInit,Input, } from '@angular/core';
import {Observable} from 'rxjs';
import {FormGroup,FormControl,FormBuilder} from '@angular/forms'
import {ActivatedRoute,Route,Router} from '@angular/router'
import {ProductType ,Product,User} from '../producttype'
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/merge';
import 'rxjs/add/operator/filter';
import 'rxjs/add/operator/debounceTime';
import 'rxjs/add/operator/distinctUntilChanged';
const states = ['Alabama', 'Alaska', 'American Samoa', 'California', 'Colorado', 'Connecticut', 'Delaware',
'Mississippi', 'Missouri', 'Montana', 'Nebraska', 'Nevada', 'New Hampshire', 'North Carolina', 'North Dakota',
'South Dakota', 'Tennessee', 'Texas', 'Utah', 'Vermont', 'Washington', 'West Virginia', 'Wisconsin', 'Wyoming'];
@Component({
selector: 'app-user-products',
templateUrl: './typeaheaddemo.component.html',
styleUrls: ['./typeaheaddemo.component.css']
})
export class TypeaheaddemoComponent implements OnInit {
@Input()
product: Product;
user: User;
//UserType = UserType;
product_name: string;
product_type: ProductType;
//ProductType = ProductType;
product_desc: string;
produktForm: FormGroup;
products: Product[];
userObj: any;
expid: string;
id: any;
products_: any;
users$: Observable<User[]>;
model: any;
@ViewChild('instance') instance: NgbTypeahead;
focus$ = new Subject<string>();
click$ = new Subject<string>();
search = (text$: Observable<string>) =>
text$
.debounceTime(200).distinctUntilChanged()
.merge(this.focus$)
.merge(this.click$.filter(() => !this.instance.isPopupOpen()))
.map(term => (term === '' ? states : states.filter(v => v.toLowerCase().indexOf(term.toLowerCase()) > -1)).slice(0, 10));
constructor(private fb: FormBuilder,
private route: ActivatedRoute,
private router: Router) { }
ngOnInit() {
//this.userObj = this.authService.currentUser;
//onst userid = this.userObj.userid;
this.produktForm = this.fb.group({
product_name: this.product_name,
product_type: this.product_type,
product_desc: this.product_desc
});
}
}
typeaheaddemo.component.html typeaheaddemo.component.html
<label for="typeahead-basic">Search for a state:</label>
<input id="typeahead-focus" type="text" class="form-control" [(ngModel)]="user" [ngbTypeahead]="search" (focus)="focus$.next($event.target.value)" (click)="click$.next($event.target.value)" #instance="ngbTypeahead" />
<hr>
<pre>Model: {{ model | json }}</pre>
app.module.ts app.module.ts
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import {NgbModule} from '@ng-bootstrap/ng-bootstrap';
import { AppComponent } from './app.component';
import {FormsModule,ReactiveFormsModule} from '@angular/forms';
import { TypeaheaddemoComponent } from './typeaheaddemo/typeaheaddemo.component'
@NgModule({
declarations: [
AppComponent,
TypeaheaddemoComponent
],
imports: [
BrowserModule,
NgbModule.forRoot(),
FormsModule,
ReactiveFormsModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
Make sure that you have imported FormsModule,ReactiveFormsModule
in your app.module otherwise ngModel and Formbuilder
would not work 确保您在
FormsModule,ReactiveFormsModule
导入了FormsModule,ReactiveFormsModule
,否则ngModel and Formbuilder
将无法正常工作
I had several modules. 我有几个模块。 Should be set in the main method
NgbModule.forRoot()
, - app.module.ts
, and in the child NgbModule
. 应该在主方法
NgbModule.forRoot()
, - app.module.ts
和子NgbModule
。
test.module.ts: test.module.ts:
import { NgModule } from '@angular/core';
// ...
import { NgbModule } from '@ng-bootstrap/ng-bootstrap';
// ...
@NgModule({
imports: [
FormsModule,
HttpModule,
ReactiveFormsModule,
NgbModule,
RouterModule.forChild(routes)
],
declarations: [
// ..
],
providers: [ProductsService],
exports: [ // ..
]
})
export class ProductModule { }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.