要在Angular 7中填充表单输入,可以使用Reactive Forms或Template-driven Forms。下面是两种解决方法的示例代码:
组件代码:
import { Component, OnInit } from '@angular/core';
import { FormGroup, FormControl } from '@angular/forms';
@Component({
selector: 'app-form',
templateUrl: './form.component.html',
styleUrls: ['./form.component.css']
})
export class FormComponent implements OnInit {
myForm: FormGroup;
ngOnInit() {
this.myForm = new FormGroup({
firstName: new FormControl(''),
lastName: new FormControl(''),
});
// 填充表单输入
this.myForm.setValue({
firstName: 'John',
lastName: 'Doe'
});
}
}
模板代码:
组件代码:
import { Component } from '@angular/core';
@Component({
selector: 'app-form',
templateUrl: './form.component.html',
styleUrls: ['./form.component.css']
})
export class FormComponent {
firstName = 'John';
lastName = 'Doe';
}
模板代码:
这两种方法都可以用来填充表单输入,并可以根据具体情况选择使用哪种方法。