Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get data from observable in angular2

I am trying to print the result of http call in Angular using rxjs

Consider the following code

import { Component, Injectable, OnInit } from '@angular/core'; import { Http, HTTP_PROVIDERS } from '@angular/http'; import 'rxjs/Rx';  @Injectable() class myHTTPService {   constructor(private http: Http) {}    configEndPoint: string = '/my_url/get_config';    getConfig() {      return this.http       .get(this.configEndPoint)       .map(res => res.json());   } }  @Component({     selector: 'my-app',     templateUrl: './myTemplate',     providers: [HTTP_PROVIDERS, myHTTPService],   }) export class AppComponent implements OnInit {      constructor(private myService: myHTTPService) { }      ngOnInit() {       console.log(this.myService.getConfig());     } } 

Whenever I tried to print out the result of getconfig it always return

Observable {_isScalar: false, source: Observable, operator: MapOperator} 

even though I return a json object instead.

How would I print out the result of getConfig ?

like image 923
testing Avatar asked Apr 04 '16 05:04

testing


People also ask

How get fetch value from Observable?

A BehaviorSubject is an Observable that always has a value, and you can call myBehaviorSubject. getValue() or myBehaviorSubject. value to synchronously retrieve the value the BehaviorSubject currently holds.

How do we get the value back from an Observable in our component?

Observable values can be retrieved from any locations. The source sequence is first pushed onto a special observer that is able to emit elsewhere. This is achieved with the Subject class from the Reactive Extensions (RxJS). Store value onto the observer.

What is the use of observable in angular?

In angular, Observables are one of the most used techniques. It is used extensively in integration with Data Services to read an API. Other than that, to access an observable, the component first needs to subscribe to the Observable.

What are rxjs and observables in angular?

This pattern can also be used in Angular 1. RxJS and Observables are not just an Angular feature. This may seem like a lot of work for a simple todo app but scale this up to a very large app and Observables can really help manage our data and application state. This pattern follows the idea of unidirectional data flow.

Is HTTP GET cold observable in angular?

The observable returned from the angular http.get are cold observable. They are not executed until you subscribe to them. Thanks for contributing an answer to Stack Overflow!

How to get data from http to form in AngularJS?

Angular is based on observable instead of promise base as of angularjs 1.x, so when we try to get data using http it returns observable instead of promise, like you did then to get data and show on view we have to convert it into desired form using RxJs functions like .map () function and .subscribe ()


1 Answers

You need to subscribe to the observable and pass a callback that processes emitted values

this.myService.getConfig().subscribe(val => console.log(val)); 
like image 72
Günter Zöchbauer Avatar answered Oct 13 '22 22:10

Günter Zöchbauer