Answer the question
In order to leave comments, you need to log in
How to get subscribe() value in service from component?
If without a request to the server, then it's easy, but the result of the request cannot be transmitted :/
You need to translate the value into the component.
@Injectable()
export class UserService {
constructor(
private auth: Auth,
private http: Http,
private authHttp: AuthHttp) {}
getUsers() {
this.authHttp
.get(APP_SERVER + 'api/users')
.map((response: Response) => response.json())
.subscribe(
(data) => {
return data;
},
(error) => {
console.log('user service error');
}
);
}
}
Answer the question
In order to leave comments, you need to log in
I don't quite understand what's wrong with you
getUsers() {
return this.authHttp
.get(APP_SERVER + 'api/users')
.map((response: Response) => response.json());
}
constructor(svc: UserService) {
svc.getUsers().subscribe((response) => console.log(response));
}
No way. I'll try to explain (don't kick much for terminology).
The bottom line is that in this example you are not returning anything from the getUser() function.
refers not to the getUser() function, but to the lambda
(data) => {
return data;
}
There is an option, and it's pretty simple. It is enough to add an EventEmitter to the service, which will send emit (data) as a result of each request. Any component that includes this service, in the constructor, subscribes to the dispatched events, after which it can update the content.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question