Unfortunately as I was storing many results on the service, I'm now stuck with many {{myservice.somevalue}}
(and myservice.someother
) etc. sprinkled over my other components.
I believe it would be nicer to return from the service to a component.
This is the code that I have on the service:
getIssue(issueName: string) {
return this.http.post(this.URL + 'issue/all', JSON.stringify({'issueName': issueName}))
.subscribe(data => this.somevalue = data.json(),
err => console.log(err));
}
So then on several other components I call functions like this.myservice.getIssue(issueName)
.
In the HTML I'd prefer to use {{somevalue}}
.
What can I do to return a http observable from the http service?
class SomeService {
someValue = new BehaviorSubject();
getIssue(issueName: string) {
return this.http.post(this.URL + 'issue/all', JSON.stringify({'issueName': issueName}))
.subscribe(data => this.somevalue.next(data.json()),
err => console.log(err));
}
}
In your component
class MyComponent {
constructor(someService:SomeService) {
someService.someValue.subscribe(val => {
this.someValue = val;
});
}
}
For an alternative approach see also https://stackoverflow.com/a/36291681/217408
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.