I'm new to Angular and still learning it.
I have a component which subscribes to the router NavigationEnd event which accesses some variables initialized in a route resolve from a service.
constructor(private route: ActivatedRoute, private router: Router) {
var that = this;
this.router.events.subscribe((event) => {
if (event instanceof NavigationEnd) {
this.nominatim = this.route.snapshot.data.meetings.nominatim;
console.log(this.nominatim);
}
});}
When I debug the code the this.nominatim variable is undefined but it does contain a value that I can log to the console.
I have found another question on stackoverflow which explains a similar sitution 'This' is undefined inside subscribe
By adding
var that = this;
before the router event subscription I can see the variables in debug.
I would prefer it if I could still use the 'this' context.
How can I get this to work?