简体   繁体   中英

How we can get response from Helper class to component and bind with HTML in angular 7?

In below is the AppointmentHelper class I have created

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

In my main component i am trying to call helper class like the below

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

But i am unable to get the data from helper class.

So could you please suggest me how we can get response from Helper class to component and bind with HTML in angular 7?

You have a mistype in:

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

also

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

have you linked servicedata in template?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM