Invoke a Service through Angular 2
up vote
0
down vote
favorite
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?

angular
add a comment |
up vote
0
down vote
favorite
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?

angular
Any error messages? I see you aren't subscribing togetDetails. Have a look at the official documentation.
– brians69
Nov 13 '16 at 0:57
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
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?

angular
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?

angular
angular
edited Nov 21 at 22:09
halfer
14.2k757106
14.2k757106
asked Nov 13 '16 at 0:52
priyanka.sarkar
11.4k3297148
11.4k3297148
Any error messages? I see you aren't subscribing togetDetails. Have a look at the official documentation.
– brians69
Nov 13 '16 at 0:57
add a comment |
Any error messages? I see you aren't subscribing togetDetails. Have a look at the official documentation.
– brians69
Nov 13 '16 at 0:57
Any error messages? I see you aren't subscribing to
getDetails. Have a look at the official documentation.– brians69
Nov 13 '16 at 0:57
Any error messages? I see you aren't subscribing to
getDetails. Have a look at the official documentation.– brians69
Nov 13 '16 at 0:57
add a comment |
2 Answers
2
active
oldest
votes
up vote
1
down vote
accepted
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
I am still getting the same error
– priyanka.sarkar
Nov 13 '16 at 1:27
@priyanka.sarkar answer edited, you also need to importHttpandResponse
– Michael
Nov 13 '16 at 1:34
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
Nov 13 '16 at 1:39
@priyanka.sarkar Added plunker link
– Michael
Nov 13 '16 at 2:09
add a comment |
up vote
1
down vote
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);
I am still getting the same error
– priyanka.sarkar
Nov 13 '16 at 1:12
Are you includinghttpin your systemJS?
– Mark Pieszak - DevHelp.Online
Nov 13 '16 at 1:14
yes i am...would you mind to demo it in plunker so that i can figure out what is that i am missing
– priyanka.sarkar
Nov 13 '16 at 1:24
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
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
I am still getting the same error
– priyanka.sarkar
Nov 13 '16 at 1:27
@priyanka.sarkar answer edited, you also need to importHttpandResponse
– Michael
Nov 13 '16 at 1:34
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
Nov 13 '16 at 1:39
@priyanka.sarkar Added plunker link
– Michael
Nov 13 '16 at 2:09
add a comment |
up vote
1
down vote
accepted
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
I am still getting the same error
– priyanka.sarkar
Nov 13 '16 at 1:27
@priyanka.sarkar answer edited, you also need to importHttpandResponse
– Michael
Nov 13 '16 at 1:34
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
Nov 13 '16 at 1:39
@priyanka.sarkar Added plunker link
– Michael
Nov 13 '16 at 2:09
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
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
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
edited Nov 13 '16 at 2:09
answered Nov 13 '16 at 1:25
Michael
1,06011214
1,06011214
I am still getting the same error
– priyanka.sarkar
Nov 13 '16 at 1:27
@priyanka.sarkar answer edited, you also need to importHttpandResponse
– Michael
Nov 13 '16 at 1:34
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
Nov 13 '16 at 1:39
@priyanka.sarkar Added plunker link
– Michael
Nov 13 '16 at 2:09
add a comment |
I am still getting the same error
– priyanka.sarkar
Nov 13 '16 at 1:27
@priyanka.sarkar answer edited, you also need to importHttpandResponse
– Michael
Nov 13 '16 at 1:34
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
Nov 13 '16 at 1:39
@priyanka.sarkar Added plunker link
– Michael
Nov 13 '16 at 2:09
I am still getting the same error
– priyanka.sarkar
Nov 13 '16 at 1:27
I am still getting the same error
– priyanka.sarkar
Nov 13 '16 at 1:27
@priyanka.sarkar answer edited, you also need to import
Http and Response– Michael
Nov 13 '16 at 1:34
@priyanka.sarkar answer edited, you also need to import
Http and Response– Michael
Nov 13 '16 at 1:34
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
Nov 13 '16 at 1:39
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
Nov 13 '16 at 1:39
@priyanka.sarkar Added plunker link
– Michael
Nov 13 '16 at 2:09
@priyanka.sarkar Added plunker link
– Michael
Nov 13 '16 at 2:09
add a comment |
up vote
1
down vote
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);
I am still getting the same error
– priyanka.sarkar
Nov 13 '16 at 1:12
Are you includinghttpin your systemJS?
– Mark Pieszak - DevHelp.Online
Nov 13 '16 at 1:14
yes i am...would you mind to demo it in plunker so that i can figure out what is that i am missing
– priyanka.sarkar
Nov 13 '16 at 1:24
add a comment |
up vote
1
down vote
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);
I am still getting the same error
– priyanka.sarkar
Nov 13 '16 at 1:12
Are you includinghttpin your systemJS?
– Mark Pieszak - DevHelp.Online
Nov 13 '16 at 1:14
yes i am...would you mind to demo it in plunker so that i can figure out what is that i am missing
– priyanka.sarkar
Nov 13 '16 at 1:24
add a comment |
up vote
1
down vote
up vote
1
down vote
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);
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);
answered Nov 13 '16 at 1:07
Mark Pieszak - DevHelp.Online
26.2k126685
26.2k126685
I am still getting the same error
– priyanka.sarkar
Nov 13 '16 at 1:12
Are you includinghttpin your systemJS?
– Mark Pieszak - DevHelp.Online
Nov 13 '16 at 1:14
yes i am...would you mind to demo it in plunker so that i can figure out what is that i am missing
– priyanka.sarkar
Nov 13 '16 at 1:24
add a comment |
I am still getting the same error
– priyanka.sarkar
Nov 13 '16 at 1:12
Are you includinghttpin your systemJS?
– Mark Pieszak - DevHelp.Online
Nov 13 '16 at 1:14
yes i am...would you mind to demo it in plunker so that i can figure out what is that i am missing
– priyanka.sarkar
Nov 13 '16 at 1:24
I am still getting the same error
– priyanka.sarkar
Nov 13 '16 at 1:12
I am still getting the same error
– priyanka.sarkar
Nov 13 '16 at 1:12
Are you including
http in your systemJS?– Mark Pieszak - DevHelp.Online
Nov 13 '16 at 1:14
Are you including
http in your systemJS?– Mark Pieszak - DevHelp.Online
Nov 13 '16 at 1:14
yes i am...would you mind to demo it in plunker so that i can figure out what is that i am missing
– priyanka.sarkar
Nov 13 '16 at 1:24
yes i am...would you mind to demo it in plunker so that i can figure out what is that i am missing
– priyanka.sarkar
Nov 13 '16 at 1:24
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f40569376%2finvoke-a-service-through-angular-2%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Any error messages? I see you aren't subscribing to
getDetails. Have a look at the official documentation.– brians69
Nov 13 '16 at 0:57