Yesub
Yesub

Reputation: 437

Update property of an Observable object (Angular)

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.

Upvotes: 1

Views: 3419

Answers (1)

Safiyya
Safiyya

Reputation: 1393

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
      })

}

Upvotes: 3

Related Questions