I have a service in which it either stores an object so I can access it in other components or requests data from a web api and returns it in a map.
Example get for the objectService
get(): any {
if (this.object == undefined || this.object == null) {
return this.http.get(this.baseUrl + "/companies/get)
.map((data: any) => {
this.object = data;
return this.object;
});
} else {
return this.object;
}
}
Using the service to set page title
ngOnInit() {
this.objectService.get().subscribe((data: any) => {
this.title.setTitle(data.name);
});
}
So this works when I first load the page, but upon navigating to the page without a reload I get the following error ...subscribe is not a function. Which makes sense since I am returning an object and not an http request.
So I tried to return just the object and not a http request.
Example get for the objectService
get(): any {
if (this.object == undefined || this.object == null) {
return this.http.get(this.baseUrl + "/companies/get)
.map((data: any) => {
this.object = data;
});
}
return this.object;
}
Using the service to set page title
ngOnInit() {
this.title.setTitle(this.objectService.get().name);
}
This now flips the issue where page refresh gives me an error saying this.objectService.get().name is undefined and when I navigate to my component without a page refresh it works just fine.
Any help on trying to get this to work would be appreciated. Thanks in advance!
Observable.of('this.object')