我使用angular 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)发送给我“未定义”
发布于 2018-07-26 21:17:15
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元素中可用,必须在input元素上使用(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" />发布于 2018-07-26 21:17:24
在订阅中移动您的属性并为其赋值。
this.ppssService.getSinglePPS(this.key)
.subscribe(res=>{
this.ppssToDisplay = res;
this.ppsForm.controls['traitement'].setValue(this.ppssToDisplay.traitement);
console.log(this.ppssToDisplay.traitement);
}); https://stackoverflow.com/questions/51539773
复制相似问题