Search code examples
angularobservableangular7rxjs6

How to return object from Observable in Angular 7


I've got a application on Angular 7 and [email protected]. I'm trying to get value from my Observable object and I don't know why it doesn't return.

There is my example code: https://stackblitz.com/edit/angular-w9ftkw

On my example html file I've got:

<hello name="{{ result.property1 }} {{ result.property2 }} {{ result.property3 }} {{ resultIsUndefined }}"></hello>
<p>
  {{err}}
</p>

but the properties ({{ result.property1 }} {{ result.property2 }} {{ result.property3 }}) of my result are not displayed.

What is wrong with that?

The res object which I'm trying to return is still type of Observable. I was trying to cast it on MyResponseClasss but doesn't effect.

And this is my real problem. Why returned object is still type of Observable.

On this part of code:

if (result) {
  result.subscribe((result: MyResponseClass) => {
    this.resultIsUndefined = false;
    res = result;
    console.log(res);
  })
  return res;
}

I want have a res with data type of MyResponseClass.


Solution

  • I solved my problem. Observable object can be created on both ways:

    • as async by using Observable<T>
    • as sync by using of<T>

    My problem was that I created Observable objects as sync on async app. That's all.