-4

I am new to Angular 2 and need help in calling service.

Here is the Plnkr I made:

https://plnkr.co/edit/wss2Jy41pYyjQUOk47es?p=preview

Put code here

halfer
  • 19,824
  • 17
  • 99
  • 186

1 Answers1

0

First of all, make sure you have the HttpModule imported and included in your ngModule.

Second, you cannot display your profile just like:

{{profile}}

since your profile has properties like username etc.

Otherwise your code looks pretty good. Since your json looks like this:

{
  "profile":
    {
      "username":"eric",
      "bio":"Cofounder of Thinkster.io, kinda looks like Peeta from the Hunger Games",
      "image":"http://i.imgur.com/S66L2XZ.jpg",
      "following":false
    }
}

I would map the incoming data like so:

.map((res: Response) => res.json().profile);

so you get a neat object to work with:

{
  "username": "eric",
  "bio": "Cofounder of Thinkster.io, kinda looks like Peeta from the Hunger Games",
  "image": "http://i.imgur.com/S66L2XZ.jpg",
  "following": false
}

Then you can display your profile with your properties, e.g like: {{profile.username}}

Here's a plunker

AT82
  • 71,416
  • 24
  • 140
  • 167
  • Thanks @AJT, worked like a charm. you saved my lot of time. :) Thanks Alot. is there any way to show jason data in a tree format ? my data is like this: {"API_MODULE":"approvals","API_RESOURCE_NAME":"","API_RELATED_MODULE":"","WM_CONCAT":"RETRIEVE"},{"API_MODULE":"users","API_RESOURCE_NAME":"alternateapproverfor","API_RELATED_MODULE":"users","WM_CONCAT(API_CRUD)":"RETRIEVE"},{"API_MODULE":"users","API_RESOURCE_NAME":"alternateapprovers","API_RELATED_MODULE":"users","WM_CONCAT(API_CRUD)":"RETRIEVE"} – gatikash bajaj Feb 13 '17 at 08:49
  • No problem, happy to help! :) Since this solved your issue, please consider accepting the answer :) http://meta.stackexchange.com/a/5235 – AT82 Feb 13 '17 at 08:50
  • Thanks @AJT, worked like a charm. you saved my lot of time. :) Thanks Alot. is there any way to show jason data in a tree format ? my data is like this: {"API_MODULE":"approvals","API_RESOURCE_NAME":"","API_RELATED_MODULE":"","WM_CONCAT":"RETRIEVE"},{"API_MODULE":"users","API_RESOURCE_NAME":"alternateapproverfor","API_RELATED_MODULE":"users","WM_CONCAT(API_CRUD)":"RETRIEVE"},{"API_MODULE":"users","API_RESOURCE_NAME":"alternateapprovers","API_RELATED_MODULE":"users","WM_CONCAT(API_CRUD)":"RETRIEVE"} – gatikash bajaj Feb 13 '17 at 08:54
  • What data is this, and how is it related to this issue? :) just check how to create a tree structure in JavaScript and that should help you. You can check this one out: http://stackoverflow.com/questions/18017869/build-tree-array-from-flat-array-in-javascript – AT82 Feb 13 '17 at 09:03
  • HI AJT, i updated my plnkr still not getting the value, can you please help ? https://plnkr.co/edit/wss2Jy41pYyjQUOk47es – gatikash bajaj Feb 13 '17 at 10:50
  • Please recreate the issue in a working plunker. I can't debug it as it is. Plunker has template for Angular 2. And I did provide a working plunker, just compare the code there with your own ;) – AT82 Feb 13 '17 at 11:01
  • Here's how you can create a Angular 2 plunker: http://imgur.com/a/5ASFa – AT82 Feb 13 '17 at 11:08
  • Hi , i added new plnkr https://plnkr.co/edit/XuVJXK1Jjlo3zjgM2awq its just stuck on loading i cant see the username on other page. !!! – gatikash bajaj Feb 13 '17 at 11:28
  • The reason why it wasn't loading was that you had no ngModule in your plunker,and the component paths were wrong. Your index file had some strange code. Check your index file. Of course the plunker index file is completely different from your actual app. So if your app isn't loading, you need to check your index file.7 – AT82 Feb 13 '17 at 12:11
  • i think i need some classes from you ;) – gatikash bajaj Feb 14 '17 at 03:58