Join egghead, unlock knowledge.

Want more egghead?

This lesson is for members. Join us? Get access to all 3,000+ tutorials + a community with expert developers around the world.

Unlock This Lesson
Become a member
to unlock all features

Level Up!

Access all courses & lessons on egghead today and lock-in your price for life.


    Load Data to Build Angular Navigation

    John LindquistJohn Lindquist
    2 - 4

    Using the Http service, you can load in data to create your routerLink navgation. This builds on the same concepts of using *ngFor, but since Http uses streams, you’ll need to use the | async pipe to be able to properly display the loaded data.



    Become a Member to view code

    You must be a Member to view code

    Access all courses and lessons, track your progress, gain confidence and expertise.

    Become a Member
    and unlock code for this lesson




    I have some "Star Wars" data at this URL. It's just an array of all the people in Star Wars. If you want to set this up yourself to follow along, just go to JohnLindquist/swap adjacent server, NPM Install and NPM Start, and you'll have your own local copy running.

    In my Contacts component, I want to load in this data. I'll say Constructor, and I need to inject the http service. To be able to use this, I'm going to have to add the http module to my Contacts module. We'll import the http module. Then, back in the component, I can load all the people by http.get, and paste in that string from my API.

    I need to map the response and parse it as JSON. Always remember, if you use a RxJS operator, you need to import it, RxJS/add/operator/map. Then I'm going to assign this to Contacts, and since this will be an observable, I'm going to add a $ to the end. It's a naming convention for that. I'll say Contacts is all of this stuff.

    That's going to be an array of people, which I can then display in this template. I'll say div ngFor let contact of Contacts async. Render out the Just made a typo. I forgot to invoke JSON. JSON's a method to take the response, get the body, and return the JSON object off of that.

    I'll hit save, and now we should see, in the Contact, an array of divs, or the divs that are parsed out and loaded, a collection of divs that each have the contacts that we loaded in and displaying the contact name.

    Let's take these divs and nest an a tag inside of it. We can set up, just like we've done before, these links to click on, and set up the router link. The router link here is just going to be the contact ID. This is going to be relative to the current URL, so we're in Contacts, and this will just append the ID to it.

    When I click on Luke, it's going to append 1 onto that. When I click on C3PO, it's going to append a 2 onto that. If I wanted to hard code this, it'd look a little bit different. It wouldn't be contact/contactID, because that wouldn't evaluate anything. It's like contacts divided by contactID.

    What you do is you make an array, and then you use strings for the parts that you know what the strings and names are, and then comma-separate the values you want to evaluate. This would evaluate to contacts/, but because we're inside of the Contacts path already, this would actually try and take us to contacts/contacts, and then contactID.

    We could solve that, if you need to work with absolute URLs, just by putting "/" in the front. This will work the same and behave as you'd expect. I'll go to Contacts, click on Darth. It will take us to Contact4, go back to Contacts.

    What makes sense most of the time is keep track of where you are inside your app. We are currently inside of the Contacts module, which is mapped to that Contacts path, so a contact ID is all we need to make sure to go to the proper /ID.