import { FormGroup, FormControl } from '@angular/forms';
export class MyComponent {
myForm: FormGroup;
constructor() {
this.myForm = new FormGroup({
firstName: new FormControl(),
lastName: new FormControl()
});
}
}
component.html:
import { ReactiveFormsModule } from '@angular/forms';
@NgModule({
imports: [
ReactiveFormsModule
]
})
export class AppModule {}
正确的写法:
import { OnInit } from '@angular/core';
export class MyComponent implements OnInit {
// ...
ngOnInit() {
this.myForm.setValue({
firstName: 'John',
lastName: 'Doe'
});
}
}
通过以上四步操作可以解决Angular响应式表单未在屏幕上显示的问题。