我们如何获得Helper类对组件的响应并在angular 7中与HTML绑定?

时间:2019-03-04 11:20:13

标签: angular angular-services

下面是我创建的 AppointmentHelper

export class AppointmentHelper {

public static bindAssessements(servicedata: any,
    appointmentService: AppointmentService,
    serviceDeliveryGuid: string): any {
    appointmentService.getAssessments(serviceDeliveryGuid)
      .subscribe((response: any) => {
        if (response && response.data) {
            this.servicedata = response.data
        }
    }, () => {
      // Error Block
    });
  }
}

在我的主要组件中,我试图像下面这样调用助手类

ngOnInit() {   
  servicedata :any;
    this.serviceDeliveryGuid = '13c22f96-163e-40cf-b13a-e6832154d985';
    AppointmentHelper.bindAssessements(this.servicedata, this.appointmentService, this.serviceDeliveryGuid);
  }

但是我无法从帮助程序类中获取数据。

所以您能建议我如何获得Helper类对组件的响应并与HTML 7绑定吗?

1 个答案:

答案 0 :(得分:0)

您输入错误的内容:

public static bindAssessements(servicedata: any,
    appointmentService: AppointmentService,
    serviceDeliveryGuid: string): any {
    appointmentService.getAssessments(serviceDeliveryGuid)
      .subscribe((response: any) => {
        if (response && response.data) {
            // here should be servicedata = response.data
            this.servicedata = response.data
        }
    }, () => {
      // Error Block
    });
  }
}

ngOnInit() {   
  servicedata :any; // why? you also using this.servicedata in function call
  ...
}

您是否已在模板中链接了服务数据?

<span>{{ servicedata }}</span>