0

I am trying to consume the service from http://jsonplaceholder.typicode.com/posts in angular 2

My html is as under

employee-information-apps.html
--------------------------------
<button (click)="getDetails()" id="tbn1">Invoke Service</button>

app.ts

//our root app component
import {Component, NgModule} from '@angular/core'
import {BrowserModule} from '@angular/platform-browser'
import { HttpModule } from '@angular/http';

// Annotation section
@Component({
  selector: 'my-app',
  template: `
    <div style="width: 50%; margin: 0 auto;">     
      <employee-selector></employee-selector>
    </div>
  `,  
})
// Component controller
export class App { 
  constructor() { }
}

// Annotation section
@Component({
  selector: 'employee-selector',
  templateUrl: 'employee-information-apps.html'   
})

// Component controller
export class EmployeeInformation {

  http: Http;
  constructor(private _http: Http) {

    this.http = http;
    this.url='http://jsonplaceholder.typicode.com/posts';


  }//end constructor      

      getDetails(){

             return this._http.get(this.url)
                    .map((response: Response) => Console.log(response.json()))
                    .catch(this.handleError);   
    } //end getDetails

}

@NgModule({
  imports: [ BrowserModule ],
  declarations: [ App,EmployeeInformation ],
  bootstrap: [ App ]
})
export class AppModule {}

But I could not make this work.

What is the mistake that I am making?

enter image description here

1
  • Any error messages? I see you aren't subscribing to getDetails. Have a look at the official documentation. Commented Nov 13, 2016 at 0:57

2 Answers 2

1

You need to import HttpModulein your NgModule

Here in your app.ts

@NgModule({
  imports: [ 
     BrowserModule,
     HttpModule // <--- here!
  ],
  declarations: [ App,EmployeeInformation ],
  bootstrap: [ App ]
})
export class AppModule {}

And also, you are missing Http and Response import.

import {Http, Response} from '@angular/http';

Example Plunker

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

3 Comments

@priyanka.sarkar answer edited, you also need to import Http and Response
No..still error.. a demo in jsfiddle or plunker will be helpful so that i can figure out what is that i am missing..
@priyanka.sarkar Added plunker link
1

You never subscribed. The observer is "cold" until activated with a subscribe.

return this._http.get(this.url)
    .map((response: Response) => Console.log(response.json()))
    .subscribe(result => {
        // this.posts = result;
    })
    .catch(this.handleError);   

2 Comments

Are you including http in your systemJS?
yes i am...would you mind to demo it in plunker so that i can figure out what is that i am missing

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.