首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >表单的SetValue

表单的SetValue
EN

Stack Overflow用户
提问于 2018-07-26 21:15:04
回答 2查看 79关注 0票数 0

我使用angular CLI1.6、angularfire2和firebase。我使用的是一个可观察对象,我想用firebase的值填充我的表单。

我的firebase接口:

代码语言:javascript
复制
export class PPS
{
constructor (public Patientid : string, public traitement: string,
public datedebut = new Date, public datefin : string, 
public description: string, public effetsind, public Esresp: string, public 
medresp: string, public contactmail: string,
public contacttel: string) {}
}

我试试这个:

代码语言:javascript
复制
this.ppssToDisplay = this.ppssService.getSinglePPS(this.key)
.subscribe(res=>{
  this.ppsForm.controls['traitement'].setValue(this.ppssToDisplay.traitement);
  console.log(this.ppssToDisplay.traitement);
});

出什么问题了?console.log(this.ppssToDisplay.traitement)发送给我“未定义”

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-07-26 21:17:15

this.ppssToDisplay是你的观察点试试这个

代码语言:javascript
复制
this.ppssToDisplay = this.ppssService.getSinglePPS(this.key)
.subscribe(res=>{
  let response = res;
  this.ppsForm.controls['traitement'].setValue(response.traitement);
  console.log(response.traitement);
});

要使该值在html元素中可用,必须在input元素上使用(ngModel)]。为此,创建一个全局类变量,比如ppssServiceData,并将此变量加载到回调中,

代码语言:javascript
复制
this.ppssToDisplay = this.ppssService.getSinglePPS(this.key)
 .subscribe(res=>{
   this.ppssServiceData = res;
   this.ppsForm.controls['traitement'].setValue(this.ppssServiceData.traitement);
   console.log(this.ppssServiceData.traitement);
});

现在,使用此变量绑定日期字段,即this.ppssServiceData,如下所示

代码语言:javascript
复制
<input type="text" .... [(ngModel)]="ppssServiceData?.YOUR_DATE_KEY" />
票数 1
EN

Stack Overflow用户

发布于 2018-07-26 21:17:24

在订阅中移动您的属性并为其赋值。

代码语言:javascript
复制
 this.ppssService.getSinglePPS(this.key)
.subscribe(res=>{
    this.ppssToDisplay = res;
    this.ppsForm.controls['traitement'].setValue(this.ppssToDisplay.traitement);
    console.log(this.ppssToDisplay.traitement);
}); 
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51539773

复制
相关文章

相似问题

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