我是使用Angular 4的新手.我正在尝试在角度4中练习模型驱动的表单,但它不断得到这个错误.
Template parse errors: Can’t bind to ‘formGroup’ since it isn’t a
known property of ‘form’. (”][formGroup] = “form” (ngSubmit)=”onSubmit(form.value)”>
“): ng:///ComponentsModule/AdsComponent.html@71:38 Error: Template parse errors: Can’t bind to ‘formGroup’ since it isn’t a known
property of ‘form’. (”][formGroup] = “form” (ngSubmit)=”onSubmit(form.value)”>
我试着搜索它如何解决的问题.大多数情况下,解决方案是在模块中导入ReactiveFormsModule.
这是我的component.ts中的代码
import { Component,Input,OnChanges,SimpleChange } from '@angular/core';
import { FormGroup,FormControl,ReactiveFormsModule } from '@angular/forms';
import { HttpClient,HttpErrorResponse } from '@angular/common/http';
@Component({
templateUrl: 'ads.component.html'
})
export class AdsComponent {
form;
ngOnInit() {
this.form = new FormGroup({
ads_name: new FormControl("Hello Ads!")
});
}
constructor(
private http: HttpClient
) {
}
onSubmit = function(user) {
console.log(user);
}
}
这是我的component.html中的代码
这是我模块中的代码
import { ReactiveFormsModule,FormsModule } from '@angular/forms';
@NgModule({
imports: [
BrowserModule,AppRoutingModule,BsDropdownModule.forRoot(),TabsModule.forRoot(),ChartsModule,ReactiveFormsModule,HttpClientModule,FormsModule
],
Template parse errors: Can’t bind to ‘formGroup’ since it isn’t a
known property of ‘form’. (” ][formGroup] = “form”
(ngSubmit)=”onSubmit(form.value)”> “):
ng:///ComponentsModule/AdsComponent.html@
我们可以假设您的AdsComponent是ComponentsModule声明的一部分,但您已在AppModule中导入了ReactiveFormsModule.所以为了使angular能够编译AdsComponent的模板你应该:
1)在ComponentsModule中导入ReactiveFormsModule:
@NgModule({
imports: [
...
ReactiveFormsModule
]
})
export class ComponentsModule {}
2)或导出ReactiveFormModule的导入模块
@NgModule({
exports: [
...
ReactiveFormsModule
]
})
export class SharedModule {}
@NgModule({
imports: [
...
SharedModule
]
})
export class ComponentsModule {}
也可以看看: