I have created an Angular 7 component that has an observable from an angular service. I am using it to bind data from a remote service to a list on one of my pages. It works pretty good. However, one thing I noticed is in the subscribe asynchronous callback if I use a named function instead of an anonymous function the data does not bind to the list on my page.
Here is an example of the current anonymous callback which works
public constructor(private vendorApiService: VendorApiService,
private eventMessagingService: EventMessagingService) {
this.vendorApiService.getVendors().subscribe(
(data) => {
this._vendor = data;
this.dataSource = new MatTableDataSource<Vendor>(this._vendor);
this.dataSource.paginator = this.paginator;
});
this._displayedColumns = ['id', 'code', 'name', 'edit'];
}
I would like to convert it to this. However, this does not work:
public constructor(private vendorApiService: VendorApiService,
private eventMessagingService: EventMessagingService) {
this.vendorApiService.getVendors().subscribe(
this.setup_vendor_list);
this._displayedColumns = ['id', 'code', 'name', 'edit'];
}
private setup_vendor_list(data) {
this._vendor = data;
this.dataSource = new MatTableDataSource<Vendor>(this._vendor);
this.dataSource.paginator = this.paginator;
}
Based on what I know about Javascript this should work. However, since this is typescript, there may be some special condition I am not aware of that impacts the way named and anonymous callbacks are handled.
If you can explain the difference, please do so.