2

I have a service with a method. This method subscribes in the service and not in the component. The result is in message.content.

Here is the code:

Service Method:

myMethod() {
    this.socket$.subscribe(
        (message) => {
          console.log(`${message.content}`);
        }
    );
}

App Component

contentVariable;
ngOnInit() {
    this.myservice.myMethod();
}

My question is...how do I get message.content from the service into contentVariable in my component?

1
  • i think your solution like this service return this.socket$.subscribe() component this.contentVariable = this.myservice.myMethod(); Commented Mar 1, 2019 at 11:36

6 Answers 6

4

You can use promises to return a value from the subscribe method.

myMethod() {

    return new Promise(resolve=>{ 
       this.socket$.subscribe(
        (message) => {
          resolve(message.content);
        }
    );
   });

  }

In your component.ts, use the promise then() method to assign the value to contentVariable

contentVariable;

  ngOnInit() {


    this.myservice.myMethod().then(val => this.contentVariable = val);


  }
Sign up to request clarification or add additional context in comments.

Comments

0

Simply return message.content from your service.

Service side:

myMethod() {
this.socket$.subscribe(
    (message) => {
      return message.content;
    });

}

Component side:

contentVariable: any;

ngOnInit() {

   this.contentVariable = this.myservice.myMethod();
}

3 Comments

This would return the Subscription and not the value
This is not a problem, we can subscribe contentVariable to get its content.
FYI, this is the behavior of your code stackblitz.com/edit/angular-rte9nv
0

I would subscribe from the component, however, you can set a property in your service:

myMethod() {

    this.socket$.subscribe(
        (message) => {
          this.content = this.message.content;
        }
    );

  }

and use that property of the service in your component.

Comments

0

You have to make use of Subject to achieve this.

Service Method:

content$ : Subject<string> = new Subject<string>()
myMethod() {
    this.socket$.subscribe(
        (message) => {
          this.content$.next(message.content)
          console.log(`${message.content}`);
        }
    );

  }

App Component

contentVariable;

ngOnInit() {   
    this.myservice.content$.subscribe(content => this.contentVariable = content)
    this.myservice.myMethod();
}

Comments

0

Your service method needs to return the observable. You can use tap to view the data without subscribing, that will allow your service to log it as you were trying to do. The component then subscribes to the returned observable. Service

import { tap } from 'rxjs/operators';
import { Observable } from 'rxjs';

// class name, constructor, etc omitted

myMethod() : Observable<{content: string}> {
    return this.socket$.pipe(tap((message) => console.log(`${message.content}`)));
}

Component

contentVariable;
ngOnInit() {
    this.myservice.myMethod().subscribe(_ => this.contentVariable = _.content);
}

Comments

0

There can be two options.

as per your code, you have to return the message result so that you can access into your component method.

In service,

myMethod() {

this.socket$.subscribe(
    (message) => {
      return message.content
    }
);

}

Edit:

If you are making any api call in service, for ex, you can subscribe like below mentioned:

service:

url = 'localhost:3000';

api = ${url}/api/get/users;

getUsers() {

return this.http.get(api)

}

component: ngOnInit() {

this.service.getUsers().subscribe((res)=>{

console.log(res)

})

}

2 Comments

How so I call it from the component?
This would return the Subscription and not the value

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.