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?



enter image description here










share|improve this question
























  • 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















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?



enter image description here










share|improve this question
























  • 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













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?



enter image description here










share|improve this question















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







angular






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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
















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












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






share|improve this answer























  • 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










  • 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


















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);





share|improve this answer





















  • 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










  • 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













Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















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

























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






share|improve this answer























  • 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










  • 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















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






share|improve this answer























  • 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










  • 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













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






share|improve this answer














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







share|improve this answer














share|improve this answer



share|improve this answer








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










  • @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










  • @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










  • @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












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);





share|improve this answer





















  • 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










  • 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

















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);





share|improve this answer





















  • 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










  • 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















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);





share|improve this answer












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);






share|improve this answer












share|improve this answer



share|improve this answer










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




















  • 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










  • 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




















 

draft saved


draft discarded



















































 


draft saved


draft discarded














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





















































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







Popular posts from this blog

Catalogne

Violoncelliste

Héron pourpré