【问题标题】:how to get value from angular 4 observable response? [duplicate]如何从角度 4 可观察响应中获取价值? [复制]
【发布时间】:2017-11-08 03:00:56
【问题描述】:

我的问题是我得到的数据是可观察的,所以我检查数据是否 未定义,当它不是未定义时,我将其放入医生对象中,但是 我的一些代码在填充并生成错误之前使用数据,因为 值未定义如何获得变量中响应的值。

    workingDayFilter = ( d: Date ): boolean => {
    const day = d.getDay();
    let workingDays = this.doctor.workingDays;
    let result: boolean = false;

    for ( let i = 0; i < workingDays.length; i++ ) {
        if ( day == workingDays[i] ) {
            result = true;
        }
    }

    return result;

}


ngOnInit() {

    this.getDoctor();


    console.log( this.doctor );
    this.appointmentForm = this.formBuilder.group( {
        patientId: this.patientId,
        date: this.date,
        timeSlot: this.timeSlot
    } );

}
public getDoctor() {
    this.doctorService.getDoctorPublicInfo().subscribe(( data ) => {
        if ( data != undefined ) {
            this.doctor = data.json();
        }
    } );
}

【问题讨论】:

  • 当你打电话给workingDayFilter ?
  • 过滤器被角度材料自动调用我无法控制它的执行它基本上添加一个过滤器到角度材料日期选择器。
  • 在分配值之前,我会查找 workingDayFilter 中未定义的属性。
  • 当时分配了属性,这是该属性的初始化,但由于医生对象中未填充值,因此未定义为值

标签: javascript angular undefined observable


【解决方案1】:

试试这样的

 ngOnInit() {

 this.getDoctor().subscribe((data) => {


    console.log(this.doctor);
    this.appointmentForm = this.formBuilder.group({
        patientId: this.patientId,
        date: this.date,
        timeSlot: this.timeSlot
    });
})

};
public getDoctor() {
 this.doctorService.getDoctorPublicInfo().map((resp) => {
    if (resp != undefined) {
        this.doctor = resp.json();
    }
});

}

【讨论】:

  • 这不起作用 角度给出错误 DoctorMakeAppointment.html:8 错误错误:formGroup 需要一个 FormGroup 实例。请传入一个,我认为它是因为 angular 没有获得 formgroup 实例。
  • 我发现了这个错误。你的html正确吗? stackoverflow.com/questions/38444778/…
  • 我的 html 是正确的渲染视图。
  • 你的答案是正确的,我只能在订阅方法中使用医生对象上面的代码不起作用,但我发现另一种方法我不能在 getDoctor 方法中使用验证器,但我可以使用过滤数据选择器并在似乎对我有用的 getdoctor 中设置其值,感谢您的帮助。
猜你喜欢
  • 1970-01-01
  • 2019-04-15
  • 1970-01-01
  • 2020-04-04
  • 1970-01-01
  • 1970-01-01
  • 2017-05-23
  • 2020-10-28
  • 2020-05-11
相关资源
最近更新 更多