如何从数据库中获取数据并将数据库中的数据设置为输入框
下面是我的一段代码
get服务
getStudentAddress() {
const id = sessionStorage.getItem('userId');
return this.http.get('http://localhost:8080' + '/student/' + id);
}返回学生数据数组的getStudentAddress(),如下所示

在我的组件中,如何获取数据并将其放在一个输入框中,以便在页面打开时自动显示数据。
ngOnInit() {
// Here should the display is from the database by using setValue or patchValue
this.studentForm.setValue({
s_pNumber: '1234',
s_address: '123',
s_address2: '123',
s_pCode: '123',
s_city: '123',
s_state: '123',
s_country: '123'
});发布于 2019-04-05 02:42:14
您应该通过订阅API调用返回可观察的值,然后使用patchValue将该值分配给您的反应性表单。我假设您的FormControl名称与该对象中的属性相同。在你的component.ts上
constructor(private crudService: crudService) { }
ngOnInit() {
this.crudService.getStudentAddress().subscribe((response: any) => {
this.studentForm.patchValue(response.data);
});
}有关将值设置为FormControl的详细信息,请参阅我的另一个answer。
https://stackoverflow.com/questions/55527496
复制相似问题