在模块中导入“ReactiveFormsModule”模块,并在相关组件的模板中添加“formGroup”指令。示例如下:
app.module.ts:
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { ReactiveFormsModule } from '@angular/forms';
import { AppComponent } from './app.component';
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
ReactiveFormsModule // 导入ReactiveFormsModule模块
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
app.component.ts:
import { Component } from '@angular/core';
import { FormBuilder, FormGroup } from '@angular/forms';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
form: FormGroup;
constructor(private fb: FormBuilder) {
this.form = this.fb.group({
// 定义表单项...
});
}
}
app.component.html:
上一篇:Angular中出现'属性'content'在类型'never'上不存在”的错误。
下一篇:Angular中出现了“Typepromise<object>ismissingthefollowingpropertiesfromtype<object>的错误提示。