I am trying to add custom content in the popup template from a service returned results. The service function is working in the ngOninit() or in a custom function which is not a part of the popup template function. When ever using in the popup custom template function, the service is failed to collect the results.
Please find the code below (included only the major part), importing the custom service.
import { CustomService } from '../shared/service/custom.service';
constructor(private customService: CustomService){}
// Formation of the popup template
var popupTrailheads = {
title: "Unique id: {ID}",
content: this.getcustomcontent,
};
forming the feature layer the popup should come from this layer.
this.layer_fifteen = new FeatureLayer({
url: `${this.esriURL}/15`,
visible: true,
outFields: ['*'],
popupTemplate: popupTrailheads
});
The below function getcustomcontent() collects the details from the service.
public getcustomcontent(feature) {
// the service code
this.customService.getIdDetails(popup_id).subscribe((posts) => {
//required to get the result from the service
}
}
When I use try-catch, it shows 'TypeError: Cannot read property 'customService' of null'. How can I use service in popup template?
I think you are having a context problem. The value of
this
insidegetcustomcontent
is null when it is executed to render the template.There are some options to set the execution context of a function. In the example below I am using
bind
.Basically, I am indicating that when
customPopupFunction
is call it should be bind to the component. That is whythis
inside the function works, and it rendersmadeBy
property of the comoponent in the popup template content.Mozilla Docs - bind