这种情况
我试图在我的Angular应用程序中创建一个非常简单的表单,但无论如何,它都无法工作。
Angular版本
Angular 2.0.0 RC5
这个错误
不能绑定到'formGroup'因为它不是'form'的已知属性
的代码
视图
<form [formGroup]="newTaskForm" (submit)="createNewTask()">
<div class="form-group">
<label for="name">Name</label>
<input type="text" name="name" required>
</div>
<button type="submit" class="btn btn-default">Submit</button>
</form>
控制器
import { Component } from '@angular/core';
import { FormGroup, FormControl, Validators, FormBuilder } from '@angular/forms';
import {FormsModule,ReactiveFormsModule} from '@angular/forms';
import { Task } from './task';
@Component({
selector: 'task-add',
templateUrl: 'app/task-add.component.html'
})
export class TaskAddComponent {
newTaskForm: FormGroup;
constructor(fb: FormBuilder)
{
this.newTaskForm = fb.group({
name: ["", Validators.required]
});
}
createNewTask()
{
console.log(this.newTaskForm.value)
}
}
《ngModule
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule } from '@angular/forms';
import { routing } from './app.routing';
import { AppComponent } from './app.component';
import { TaskService } from './task.service'
@NgModule({
imports: [
BrowserModule,
routing,
FormsModule
],
declarations: [ AppComponent ],
providers: [
TaskService
],
bootstrap: [ AppComponent ]
})
export class AppModule { }
这个问题
为什么会出现这个错误?我遗漏了什么吗?
对于Angular 14和使用带有路由的子模块的人
我的问题与使用带有路由的子模块有关。
要做到这一点,请遵循以下三个步骤:
1-在app.module中
imports: [
//other-modules
ReactiveFormsModule
]
2-在子模块中
imports: [
//other-modules
ReactiveFormsModule
]
3-在应用路由中。模块添加以下路由(我的模块名为AuthModule)
{
path: 'signup',
component: SignUpComponent,
children: [
{
path: '',
loadChildren: () =>
import('./features/auth/auth.module').then((m) => m.AuthModule),
},
]
},
希望有一天这能帮助到别人!
如果你在开发一个组件时遇到了这个问题,你应该把这两个模块添加到你最近的模块中:
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
@NgModule({
declarations: [
AppComponent
],
imports: [
// other modules
FormsModule,
ReactiveFormsModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
如果你正在为你的组件开发一个测试,那么你应该像这样将这个模块添加到你的测试文件中:
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { ContactusComponent } from './contactus.component';
import { ReactiveFormsModule } from '@angular/forms';
describe('ContactusComponent', () => {
let component: ContactusComponent;
let fixture: ComponentFixture<ContactusComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ContactusComponent],
imports:[
ReactiveFormsModule
]
})
.compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(ContactusComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
});
注意:小心加载器和最小化(Rails env.):
在看到这个错误并尝试了每个解决方案之后,我意识到我的html加载器有问题。
我已经设置了我的Rails环境,为我的组件成功导入HTML路径与这个加载器(config/loaders/ HTML .js.):
module.exports = {
test: /\.html$/,
use: [ {
loader: 'html-loader?exportAsEs6Default',
options: {
minimize: true
}
}]
}
经过几个小时的努力和无数的ReactiveFormsModule导入,我看到我的formGroup是小字母:formGroup。
这让我看到了加载器,它在最小化上减少了我的HTML。
在改变了选项之后,一切都正常了,我又可以回去哭了。
我知道这不是问题的答案,但对于未来的Rails访问者(和其他使用自定义加载器的人),我认为这可能会有帮助。
对于Angular 14和使用带有路由的子模块的人
我的问题与使用带有路由的子模块有关。
要做到这一点,请遵循以下三个步骤:
1-在app.module中
imports: [
//other-modules
ReactiveFormsModule
]
2-在子模块中
imports: [
//other-modules
ReactiveFormsModule
]
3-在应用路由中。模块添加以下路由(我的模块名为AuthModule)
{
path: 'signup',
component: SignUpComponent,
children: [
{
path: '',
loadChildren: () =>
import('./features/auth/auth.module').then((m) => m.AuthModule),
},
]
},
希望有一天这能帮助到别人!