Javascript Angular 6:无法绑定到“formGroup”,因为它不是“form”的已知属性?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/50964204/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Angular 6: Can't bind to 'formGroup' since it isn't a known property of 'form'?
提问by Shubham Verma
I have worked with form builder in angular 2/4, But now I am using it in angular 6. I have seen this question (Can't bind to 'formGroup' since it isn't a known property of 'form') but it was for angular 2. I doing exact same against angular 4 but I am getting this error. Please help: my code are:
我曾在 angular 2/4 中使用表单构建器,但现在我在 angular 6 中使用它。我见过这个问题(无法绑定到 'formGroup',因为它不是 'form' 的已知属性)但是它是针对 angular 2 的。我对 angular 4 做的完全一样,但我收到了这个错误。请帮助:我的代码是:
app.module.ts: ( I have exported FormsModule & ReactiveFormsModule) :
app.module.ts: ( 我已经导出 FormsModule & ReactiveFormsModule ) :
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { LocalStorage } from './services/localStorage.service';
import { HttpModule, RequestOptions, XHRBackend } from '@angular/http';
import { Router } from '@angular/router';
import { RouterModule } from '@angular/router';
import { AppComponent } from './app.component';
import { routing } from './app.route';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
import { LoginComponent } from './components/login/component';
import { ForgotComponent } from './components/forgotPassword/component';
@NgModule({
exports: [
FormsModule,
ReactiveFormsModule
],
declarations: [
AppComponent,LoginComponent,ForgotComponent
],
imports: [
BrowserModule,
routing,
],
providers: [
LocalStorage,
],
bootstrap: [AppComponent],
})
export class AppModule { }
login.component.ts:
登录.component.ts:
import { Component, ViewContainerRef, OnInit } from '@angular/core';
import { Router } from '@angular/router';
import { FormBuilder, FormGroup, Validators, NgForm } from '@angular/forms';
import { LocalStorage } from '../../services/localStorage.service';
import { environment } from '../../../environments/environment';
import { HttpService } from '../../services/http.service';
import { emailRegexp, passwordRegexp } from '../../constants';
@Component({
selector: 'login-app',
templateUrl: './login.component.html',
styleUrls: ['./login.component.scss']
})
/**
* LoginComponent class
*/
export class LoginComponent {
private loginForm: any;
loginValue:any;
constructor(
private formBuilder: FormBuilder,
private _router: Router,
private httpService: HttpService,
private localStorage: LocalStorage,
) {
this.loginValue = new LocalStorage(environment.localStorageKeys.ADMIN).value;
this.loginForm = this.formBuilder.group({
email: ['', Validators.compose([Validators.required, Validators.pattern(emailRegexp)])],
password: ['', Validators.compose([Validators.required, Validators.minLength(8)])]
});
}
}
login.component.html: ( Something like this)
login.component.html : ( 像这样的东西)
<form [formGroup]="loginForm" (ngSubmit)="loginForm.valid && login()" novalidate>
<input type="email" autocomplete="off" (focus)="focusFunction()" placeholder="User Name" formControlName="email" class="form-control">
<div class="col-12">
<input autocomplete="off" type="password" (focus)="focusFunction()" placeholder="Password" formControlName="password" class="form-control">
</div>
<button [disabled]="!loginForm.valid" type="submit" class="btn btn-inverse btn-rounded btn-block">
<div *ngIf="!loading" class="sign-in">Sign in</div>
</button>
</form>
回答by Prashant Pimpale
Add below code in your app.module.ts:
在您的app.module.ts:
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
and in imports array:
并在导入数组中:
imports: [
BrowserModule,
FormsModule,
ReactiveFormsModule
]
The FormGroupis a selector for the FormGroupDirectiveDirective which mainly used to Binds an existing FormGroupto a DOM element and FormGroupDirectiveis available in the ReactiveFormsModulemodule.
的FormGroup是对于一个选择FormGroupDirective指令,它主要用于绑定的现有FormGroup的DOM元素和FormGroupDirective是可用的在ReactiveFormsModule模块。
回答by MetaSimian
So fiddling around with the same frustrating issue -- a clean angular-cli install and a custom subcomponent/module (component.html... ) and the same error ("Can't bind to 'formGroup' since it isn't a known property of 'form' "). Angular CLI: 7.2.3, Node: 10.9.0, OS: win32 x64, Angular: 7.2.2
所以摆弄同样令人沮丧的问题——一个干净的 angular-cli 安装和一个自定义的子组件/模块(component.html...)和同样的错误(“无法绑定到‘formGroup’,因为它不是一个'形式'的已知属性”)。Angular CLI:7.2.3,节点:10.9.0,操作系统:win32 x64,Angular:7.2.2
I finally got it to workbased on the above but with a twist I put the FormsModule & ReactiveFormsModule imports in app-routing.module.ts(not app.module.ts) + the subcomponent's ts (in my case: forms-demo.component.ts) :
我终于根据上面的内容让它工作了,但是我把 FormsModule 和 ReactiveFormsModule 导入放在app-routing.module.ts(不是 app.module.ts)+ 子组件的 ts(在我的例子中:forms-demo。组件.ts):
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
....
@NgModule({
imports: [
RouterModule.forRoot(routes),
FormsModule, ReactiveFormsModule
....
Afterward ng build/serve worked without any errors.
之后 ng build/serve 工作没有任何错误。
I'm not an Angular expert but my guess is that the v7 approach whereby app.module.ts delegates to app-routing, that latter file is where the app & component imports & dependencies take place.... YMMV but hopefully it helps.
我不是 Angular 专家,但我的猜测是 v7 方法 app.module.ts 委托应用程序路由,后一个文件是应用程序和组件导入和依赖项发生的地方.... YMMV 但希望它有所帮助.
回答by Zameer Fouzan
Similar to @gloomy.penguin's answer in comment, but a different scenario with the same error.
与@gloomy.penguin 在评论中的回答类似,但具有相同错误的不同场景。
In my case, I was using a second module and its own routing file.
就我而言,我使用的是第二个模块和它自己的路由文件。
│ app-routing.module.ts
│ app.module.ts
│
└───second-module
│ second-routing-module.module.ts
│ second.module.ts
│
└───component-1
I had to import the following in second.module.tsrather than app-routing.module.ts
我不得不在second.module.ts而不是app-routing.module.ts 中导入以下内容
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
import { BrowserModule } from '@angular/platform-browser';
....
@NgModule({
imports: [
CommonModule,
FormsModule,
ReactiveFormsModule,
BrowserModule
]})
anyone using a second module, please follow this.
任何使用第二个模块的人,请按照此操作。
回答by James Siva
import these things in your appmodule.ts
imports: [
BrowserModule,
FormsModule,
ReactiveFormsModule
],
回答by hritika panchratan
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { BrowserAnimationsModule } from "@angular/platform-browser/animations";
import { MatButtonModule, MatCheckboxModule , MatTableModule, MatDialogModule, MatTabsModule, MatNativeDateModule, MatInputModule, MatSelectModule, MatFormFieldModule} from "@angular/material";
import { MyDialogComponent } from './my-dialog/my-dialog.component';
import {MatDatepickerModule} from '@angular/material/datepicker';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
import {NgxMaterialTimepickerModule} from 'ngx-material-timepicker';
import { CommonModule } from '@angular/common';
import { HighlightDirective } from './highlight.directive';
@NgModule({
declarations: [
AppComponent,
MyDialogComponent,
HighlightDirective,
],
imports: [
BrowserModule,
BrowserAnimationsModule,
MatButtonModule,
MatCheckboxModule,
MatTableModule,
MatDialogModule,
MatTabsModule,
MatNativeDateModule,
MatInputModule,
MatSelectModule,
MatDatepickerModule,
FormsModule,
ReactiveFormsModule,
NgxMaterialTimepickerModule,
CommonModule,
MatFormFieldModule,
AppRoutingModule
],
回答by Arun
Can't bind to 'formGroup' since it isn't a known property of 'form'? why means ? Answer: ' ReactiveFormsModule ' could not load in your component.you just add component name into rootmodule (app.module.ts)
无法绑定到“formGroup”,因为它不是“form”的已知属性?为什么是指?答案:“ ReactiveFormsModule ”无法加载到您的组件中。您只需将组件名称添加到 rootmodule (app.module.ts) 中
SOLUTION:
解决方案:
**WITHOUT ROUTING**
**app.modules.ts**
STEP1 : import { FormsModule,ReactiveFormsModule } from '@angular/forms';
STEP2 : check your component (AppComponent)loaded or not
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
AppRoutingModule,
ReactiveFormsModule,
FormsModule
],
providers: [],
bootstrap: [AppComponent]
})
app.component.html
use and check <form [formGroup]="registerForm"></form>
**WITH ROUTING**
Eg;I have 2 files - form.component.html,form.component.ts
STEP1:Import and add component(FormComponent) loaded or not in app.component.ts(rootcomponent) below here.
import { FormComponent } from "./form/form.component";
@NgModule({
declarations: [
AppComponent,
FormComponent
],
imports: [
BrowserModule,
AppRoutingModule,
ReactiveFormsModule,
FormsModule
],
providers: [],
bootstrap: [AppComponent]
});


