Invoke a Service through Angular 2 -


i trying consume service http://jsonplaceholder.typicode.com/posts in angular 2

my html under

employee-information-apps.html -------------------------------- <button (click)="getdetails()" id="tbn1">invoke service</button> 

app.ts

//our root app component import {component, ngmodule} '@angular/core' import {browsermodule} '@angular/platform-browser' import { httpmodule } '@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 not make work.

what mistake making? .thanks in advance.

enter image description here

you need import httpmodulein ngmodule

here in app.ts

@ngmodule({   imports: [       browsermodule,      httpmodule // <--- here!   ],   declarations: [ app,employeeinformation ],   bootstrap: [ app ] }) export class appmodule {} 

and also, missing http , response import.

import {http, response} '@angular/http'; 

example plunker


Comments

Popular posts from this blog

php - How to display all orders for a single product showing the most recent first? Woocommerce -

asp.net - How to correctly use QUERY_STRING in ISAPI rewrite? -

angularjs - How restrict admin panel using in backend laravel and admin panel on angular? -