I want to lazy load data for cacader. But failed because in the load function this=undefined. The function is defined in component, and other functions in the component working well. Please help, thanks.
<nz-cascader
\[nzLoadData\]\="loadCityBuildingData"
\[(ngModel)\]\="selectedLocation"
(ngModelChange)\="onNewLocationModalChanges($event)"
\> </nz-cascader>
loadCityBuildingData(node: NzCascaderOption, index: number): PromiseLike<void\> {
console.log(this);
return new Promise(resolve => {
if (index < 0) {
this.cityService.getCities().subscribe(item => {
const cities = \[\];
item.forEach(city => {
cities.push({value: city.id, label: city.name});
});
node.children \= cities;
resolve();
});
} else if (index === 0) {
this.buildingService.getBuildingsByCityId(node.value).subscribe(item => {
const buildings = \[\];
item.forEach(building => {
buildings.push({value: building.id, label: building.name, isLeaf: true});
});
node.children \= buildings;
resolve();
});
}
});
}
My name's Wendell and I am the author of the component. ;)
This is a frequestly asked question. The reason is that:
loadCityBuildingData to nzLoadData, loadCityBuildData becomes a property of NzCascaderComponent.nzLoadData actually), this in loadCityBuildingData is bound to nothing.this in loadCityBuildingData before you pass it.
Function.bind.A solution I found that works for me is to define your function like following :
loadCityBuildingData = (node: NzCascaderOption, index: number): PromiseLike<void> => {
...your code
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With