表单的SetValue

时间:2018-07-26 13:15:04

标签: angular asynchronous angularfire2 angular-reactive-forms

我使用角度CLI1.6,angularfire2和firebase。 我使用可观察对象,并且我想用firebase的值填充表单。

我的Firebase界面:

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) {}
}

我尝试这个:

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)向我发送“未定义”

2 个答案:

答案 0 :(得分:1)

this.ppssToDisplay是您可观察的尝试

this.ppssToDisplay = this.ppssService.getSinglePPS(this.key)
.subscribe(res=>{
  let response = res;
  this.ppsForm.controls['traitement'].setValue(response.traitement);
  console.log(response.traitement);
});

要使值在html元素中可用,必须在输入元素上使用(ngModel)]。为此,将一个全局类变量设为ppssServiceData并将其加载到您的回调中,如

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)将您的日期字段绑定

<input type="text" .... [(ngModel)]="ppssServiceData?.YOUR_DATE_KEY" />

答案 1 :(得分:1)

在订阅中移动您的媒体资源,然后为其分配值。

 this.ppssService.getSinglePPS(this.key)
.subscribe(res=>{
    this.ppssToDisplay = res;
    this.ppsForm.controls['traitement'].setValue(this.ppssToDisplay.traitement);
    console.log(this.ppssToDisplay.traitement);
});