0

Coming from the Angular1x background.I am migrating my existing app to Angular4

This is how my ng4 Service looks like

import { Injectable } from '@angular/core';
import {Http} from '@angular/http';

@Injectable()
export class DataService {
  private _http : Http;

constructor(http:Http) {
   this._http = http;
}


public GetPosts() : any{
this._http.get("https://jsonplaceholder.typicode.com/posts").subscribe(data => {
  const posts = data.json();
  console.log(posts); // logs the desired json
  return posts;
})}}

Consuming the above service from the component.

import { Component, OnInit } from '@angular/core';
import {Customer} from './customer.model';
import  {DataService } from '../../providers/data.service';

@Component({
 selector: 'app-customer',
 templateUrl: './customer.component.html',
 styleUrls: ['./customer.component.css']
})

export class CustomerComponent implements OnInit {

private _dService: DataService;

constructor(dService:DataService) {
 this._dService = dService;}

ngOnInit() {}


public GetAll(){
  let posts =this._dService.GetPosts();
  debugger;
  console.log(posts); // undefined
 /* here the posts is getting UNDEFINED (error) */
}}

In Angular1X, I used to return promise from ngService but how do the same in angular4??

3
  • you don't need to decalare private _dService: DataService; in the first line of your component, just in the constructor is enough. try witouht Commented Nov 30, 2017 at 14:40
  • @AnouarMokhtari, yeap. Thanks :) Commented Nov 30, 2017 at 14:42
  • do you use angular-CLI ? Commented Nov 30, 2017 at 14:56

3 Answers 3

4

You should subscribe to the observable in the component, not in the service.

In your service

public GetPosts() : any{
   return this._http.get("https://jsonplaceholder.typicode.com/posts");
}

And in your component

this._dService.GetPosts().subscribe(data => {
    const posts = data.json();
    console.log(posts);
    // Do whatever you like with posts

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

1 Comment

Thanks ALex, will mark your post accepted once allowed, Please upvote my post if you find it worth :)
0

The call to subscribe should be in the component's code

1 Comment

Yes.You're right. just tried with your inputs. json captured, Silly of mine. Thanks :)
0
constructor(private http:Http) { }

getPosts(){
    return this.http.get('https://jsonplaceholder.typicode.com/posts').map(
      (response: Response) => {
                    return response.json();
      }
    )
 }

And in the component: the best practice you declare :

data : any; 

this._dService.GetPosts().subscribe(
      data => {this.data = data;
      console.log(this.books);},
      err => {console.log(err);},

      ()=> {console.log("terminated");}
    );

3 Comments

.map() seems unavailble in angular4 :)
you have to import RXJS library. import 'rxjs/Rx'; in your service
it s awesome lib import 'rxjs/add/operator/map';

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.