🎁

12 Days of Baddass Courses sale! Get instant access to the entire egghead library of courses and lessons for 58% off.

Runs out in:
13 : 09 : 04 : 11
Join egghead, unlock knowledge.

Want more egghead? It's 58% off for a limited time only!

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

Unlock All Content for 40% Off
1×
Become a member
to unlock all features
Autoplay

    Define and read route parameters with the Angular router

    Juri StrumpflohnerJuri Strumpflohner
    angularAngular
    ~7.0.0

    An Angular URL can be structured into segments, each of which is formed by its path and series of additional information such as parameters, query parameters and fragment. Parameters are a way to provide a per segment scoped information, such as the ID of an object to be visualized by the activated component. In this lesson we learn how to define and read route parameters from within an Angular component in order to further process it.

    Code

    Code

    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
    Discuss

    Discuss

    Transcript

    Transcript

    Instructor: We have here a very simple route defined here in our people routing module. Whenever we point our browser's URL bar to this path here, we get the person detail component here visualized. What we would like to look at now, is how we can add variable parts to that route, which help us transport data in our URL.

    One way to do that is by using so-called router parameters. You can define them by starting with a call, and then by giving them some kind of name, like ID, or it's a person ID. In this example here, this could be basically from the whole list of people -- show me the person with that kind of ID.

    This is just the first step, basically to define that variable part. The next step is then to go into our person detail component and to actually read that information in order to display the person that matches that ID onto our component. The first step here is to get ahold of an instance of the activated route.

    Let's inject that here, and we need to import that from the Angular router here. Then within in our constructor here, or directly within our ngOnInit, we can then access this active route member variable here. We can then query the params part, which is an observable. We can subscribe to it.

    Inside here, we will get the object which contains actually our parameters. Let's quickly log that to our console. You can see now we get an object with person ID, which is the parameter we specified in our routing. Its according value in this case is path one, because we have specified this as our current URL.

    You could add here, 1234. As you can see now, we got a parameter here, correctly displayed in this object. As a result, we could simply go here -- into our detail components template -- and say, display person with ID, person ID. We can add this as a member variable here, person ID.

    Below here, we could say this.personId is equal to data. Let's add it here, personId. You can see it gets displayed properly in our template. As always, you can access this in different kind of manners. Like here, we directly subscribed to our parameters observable.

    However, if you know for sure that your component gets reloaded each time your route actually changes, you can also access the value via the snapshot. We could simply do something like this.personId equals thisActiveRoute.snapshot.params. Then here again, indicate the person ID, and that will work as well, just as we expect.

    We can say here, 222, and it would correctly refresh in our template.