I have a page with Leaflet map on it which i initialize in ionViewDidLoad()
.Also I have a components that work with map, so i need to pass map instance through @Input()
.But when I try ngOnInit()
or other angular’s strat-up hooks, the map is not initialized and I have ```undefined`` in my component.
P.s. ngOnChanges()
is not an option.
For now i use Events
that is not a good solution IMHO.
You’re giving the component data before the data is ready? Instead you could use an Observable and give the data when it is ready.
Thanks, Can you give an example?
Did you mean this aproach?
export class VehiclesForMapComponent implements OnInit {
mapInitialized: boolean;
private _map = new BehaviorSubject<L.Map>(null);
@Input()
set map(value) {
this._map.next(value);
};
get map() {
return this._map.getValue();
}
ngOnInit() {
this.mapInitialized = false;
this._map.filter(map => !!map).takeWhile(() => !this.mapInitialized).subscribe(map => {
this.mapInitialized = true;
...
});
}
}
That’s inside the component. And I don’t understand the logic there – seems the filter should be inside the set, not the initialization. That way you don’t deal with pathological values of map at all. But I meant that the containing page should only send the component defined information. For example an Observable with a filter, and use the async pipe to pass info to the component.
One thing to note is that ngOnChanges()
is the only place you can rely on @Input
properties receiving their values. ngOnInit()
= no good.
1 Like