如何从数据库中获取数据并将数据库中的数据设置到输入框中
以下是我的一段代码
获得服务
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'
});
您应该通过订阅 API 调用来返回可观察值,然后使用 patchValue 将, 分配给您的反应式表单。我假设您的 FormControl 名称与该对象中的属性相同。在你的 component.ts 上,
constructor(private crudService: crudService) { }
ngOnInit() {
this.crudService.getStudentAddress().subscribe((response: any) => {
this.studentForm.patchValue(response.data);
});
}
有关为 FormControl 设置值的更多详细信息,您可以参考我的另一个答案。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句