How to return value as an observable in angular 6

return value from subscribe angular 6
angular 6 observable example
angular 7 observable example
get data from observable angular 7
how to get value outside typescript subscribe function
get value from observable angular
angular observable return value
observable not working in angular 6

I want to return value (any data) from service to component as an observable. After a couple of digging into observables found following solution:

class AppService {
    getData(value) {
        // do we have any other best way to return value as an observable
        return Observer.create((observer) => {
            observer.next(value);
        });
    }
}

class AppComponent implements OnInit {
    ngOnInit() {
        this.dataService$ = this.appService.getData().subscribe((data) => {
            // do some task with data
        });
    }
}

Just return like below

import { Observable, of } from 'rxjs';

...

getData(value) : Observable<any> {
   // Simple way of sending value using of operator.
   return Observable.of(value);
}

Hope this help!

Using observables to pass values, The subscribe() call returns a Subscription object that has an unsubscribe() method, which you call to stop receiving notifications. Here's an example that� User clicks the link. NET Core + SQL on Azure - How to Deploy a Full Stack App to Microsoft Azure. Min method will take in an observable with all values and return an observable with the min value. The Angular 6 application will send an HTTP GET request to fake rest API. The simplest form of Observable. take(10); var timer2 = Rx.

Use of like so:

import { of } from 'rxjs';
...
return of(value);

which is equivalent to:

return new Observable(obs => obs.next(value));

However, if you want to convert a given value (e.g. a Promise, an Observable-like, an Array, an iterable or an array-like object) you may want to use from:

import { from } from 'rxjs';
...
return from(value);

Understanding, creating and subscribing to observables in Angular , If you come from the world of promises this is a key difference as promises always return only one value. Another thing is that observables are� The getUsers () function will return an observable and that observable is then handled by the calling function. So, our app.component.ts file looks like this.

Here's what I do

In your service file :

First I declare my observable this way

  myObs: BehaviorSubject<any> = new BehaviorSubject([]);

In a method you can set your datas into your obs :

var myArray = [1,2,3,4,5];
this.myObs.next(myArray);

If you want your "controller" to subscribe this obs, in your service, just expose a getter :

public getMyObs(){
return this.myObs.asObservable()
}

And in your "controller" you can call it this way :

 ngOnInit() {

this.myService.getMyObs().subscribe(datas => {
    Do What you want with your datas
  }
}

HTTP Example with Observables • Angular, Each value of this observable is a new list of todos. What this type of subject does it that it will return upon subscription the last value of the� There are many ways to create observable in Angular. You can make use of Observable Constructor as shown in the observable tutorial. There are a number of functions that are available which you can use to create new observables. These operators help us to create observable from an array, string, promise, any iterable, etc.

How to build Angular App with Observable Data Services, Returns an Observable instance that synchronously delivers the values provided as arguments. from(iterable). Converts its argument to an� When version 2 of Angular came out, it introduced us to observables. The Observable isn’t an Angular specific feature, but a new standard for managing async data that will be included in the ES7

Angular 9 Observables Example, Observables are lazy Push collections of multiple values. and the code that calls the function is consuming it by "pulling" out a single return value from its call. Angular Component is used to present data and delegate data access to a service. Angular Services are a great way to share data among classes. So in the tutorial, we introduce how to create Angular Service and use Observable class of RxJS library for asynchronous operation in Angular Service’s implementation. Related posts: – Angular 6 … Continue reading "Angular 6 Service – with

Observable, subscribe vs observable return from subscribe angular 6. I dont know how to extract value from Observable to be returned by function in which Observable is� 5. Observable subscribe subscribe is a method of Observable class.subscribe is used to invoke Observable to execute and then it emits the result. If we have an Observable variable that fetches data over an HTTP then actual hit to server takes place only when we subscribe to Observable using subscribe method or async pipe.

Comments
  • return of(value)
  • You already found the answer, what's the question here ?
  • thanks it worked, do you know if I need to do either way means returning observable as a value;
  • yes you need to return observable as value so that you can subscribe to obsevable to get data in component