1

I'm about to ask a probably dummy question, but I really have issues with Observables, so I try.
I have an object which I want to be an Observable. Let's assume that the Object is of kind example like this :

export class Example {
  public property1: string;
  public property2: string;
}

In a component.ts I have something like this :

export class Component {
  public myExample: Observable<Example>;

  constructor(){}

  myFunction() {
    this._translateService.service
     .get('SOMETHING')
     .subscribe(response => 
       myExample.property1 = response);
  }
}

The _translateService is returning an Observable.

This example is not working. How can I implement something to reproduce this behavior ?

Thanks for any help.

1 Answer 1

3

Use the map operator :

export class Component {
  public myExample: Observable<Example>;

  constructor(){}

  myFunction() {
    this.myExample = this._translateService.service
     .get('SOMETHING')
     .map(response => {
          let example = new Example();
          example.property1 = response;
          return example
      })

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

7 Comments

Hi @Safiyya thanks for taking time for answering this. But your solution doesn't seems to work. The myExample.property1 is not reachable in that way.
Sorry i forgot to create a new Example before assigning property, updated answer now
I got your point there. But at this point the variable myExample has not been modified in any way. The let example variable is not Observable.
Maybe i dont understand your question ... do you want to take an Example and the Service, smash them together and get a Example observable as a result that you can subscribe to?
Oups. I hope you won't be crossed with me... I misreaded your answer and miss the this.myExample = part of it. Your answer is good and working :-)
|

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.