首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何从数据库中获取数据并使用setValue或patchValue

如何从数据库中获取数据并使用setValue或patchValue
EN

Stack Overflow用户
提问于 2019-04-05 02:39:27
回答 1查看 1.3K关注 0票数 0

如何从数据库中获取数据并将数据库中的数据设置为输入框

下面是我的一段代码

get服务

代码语言:javascript
复制
getStudentAddress() {
 const id = sessionStorage.getItem('userId');
 return this.http.get('http://localhost:8080' + '/student/' + id);
}

返回学生数据数组的getStudentAddress(),如下所示

在我的组件中,如何获取数据并将其放在一个输入框中,以便在页面打开时自动显示数据。

代码语言:javascript
复制
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'
});
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-04-05 02:42:14

您应该通过订阅API调用返回可观察的值,然后使用patchValue将该值分配给您的反应性表单。我假设您的FormControl名称与该对象中的属性相同。在你的component.ts上

代码语言:javascript
复制
constructor(private crudService: crudService) { }

ngOnInit() {
  this.crudService.getStudentAddress().subscribe((response: any) => {
    this.studentForm.patchValue(response.data);
  });   
}

有关将值设置为FormControl的详细信息,请参阅我的另一个answer

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55527496

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档