Add content for lesson 11. Update previous lessons and code examples be in line with the latest lesson formatting. PR Close #49980
8.2 KiB
Lesson 11 - Integrate details page into application
This tutorial lesson demonstrates how to connect the details page to your app.
Time required: expect to spend about 20 minutes to complete this lesson.
Before you start
This lesson starts with the code from the previous lesson, so you can:
- Use the code that you created in Lesson 10 in your integrated development environment (IDE).
- Start with the code example from the previous lesson. Choose the from Lesson 10 where you can:
- Use the live example in StackBlitz, where the StackBlitz interface is your IDE.
- Use the download example and open it in your IDE.
If you haven't reviewed the introduction, visit the Introduction to Angular tutorial to make sure you have everything you need to complete this lesson.
If you have any trouble during this lesson, you can review the completed code for this lesson, in the for this lesson.
After you finish
At the end of this lesson your application will have support for routing to the details page.
Conceptual preview of routing with route parameters
In the previous lesson, you added routing to your app and in this lesson you will expand the types of routing your app supports. Each housing location has specific details that should be displayed when a user navigates to the details page for that item. To accopmlish this goal, you will need to use route parameters.
Route parameters enable you to include dynamic information as a part of your route URL. To identify which housing location a user has clicked on you will use the id property of the HousingLocation type.
Lesson steps
Perform these steps on the app code in your IDE.
Step 1 - Create a new service for your app
In lesson 10, you added a second route to src/app/routes.ts, this route includes a special segment that identifes the route parameter, id:
<code-example format="javascript" language="javascript">
'details/:id'
</code-example>
In this case, :id is dynamic and will change based on how the route is requested by the code.
-
In
src/app/housing-location/housing-location.component.ts, add an anchor tag to thesectionelement and include therouterLinkdirective:The
routerLinkdirective enables Angular's router to create dynamic links in the application. The value assigned to therouterLinkis an array with two entries: the static portion of the path and the dynamic data. -
At this point you can confirm that the routing is working in your app. In the browser, refresh the home page and click the "learn more" button for a housing location.
Step 2 - Get route parameters
In this step, you will get the route parameter in the DetailsComponent. Currently, the app displays details works!, next you'll update the code to display the id value passed using the route parameters.
-
In
src/app/details/details.component.tsupdate the template to import the functions, classes and services that you'll need to use in theDetailsComponent: -
Update the
template: `<p>details works! {{ housingLocationId }}</p>`,templateproperty of the@Componentdecorator to display the valuehousingLocationId: -
Update the body of the
export class DetailsComponent { route: ActivatedRoute = inject(ActivatedRoute); housingLocationId = -1; constructor() { this.housingLocationId = Number(this.route.snapshot.params['id']); } }DetailsComponentwith the following code:This code give the
DetailsComponentaccess to theActivatedRouterouter feature that enables you to have access to the data about the current route. In the constructor, the code converts the id parameter from the route to a number. -
Save all changes.
-
In the browser, click on one of the housing location "learn more" links and confirm that the numeric value displayed on the page matches the
idproperty for that location in the data.
Step 3 - Customize the DetailComponent
Now that routing is working properly in the application this is a great time to update the template of the DetailsComponent to display the specific data represented by the housing location for the route parameter.
To access the data you will add a call to the HousingService.
-
Update the template code to match the following code:
Notice that the
housingLocationproperties are being accessed with the optional chaining operator?. This ensures that if thehousingLocationvalue is null or undefined the application doesn't crash. -
Update the body of the
DetailsComponentclass to match the following code:Now the component has the code to display the correct information based on the selected housing location. The constructor now includes a call to the
HousingServiceto pass the route parameter as an argument to thegetHousingLocationByIdservice function. -
Copy the following styles into the
src/app/details/details.component.cssfile: -
Save your changes.
-
In the browser refresh the page and confirm that when you click on the "learn more" link for a given housing location the details page displays the correct information based on the data for that selected item.
Step 4 - Add navigation to the HomeComponent
In a previous lesson you updated the AppComponent template to include a routerLink. Adding that code updated your app to enable navigation back to the HomeComponent whenver the logo is clicked.
-
Confirm that your code matches the following:
Your code may already be up-to-date but confirm to be sure.
Lesson Review
In this lesson you updated your app to:
- use route paramters to pass data to a route
- use the
routerLinkdirective to use dynamic data to create a route - use route paramter to retrieve data from the
HousingServiceto display housing location specific information.
Really great work so far.
If you are having any trouble with this lesson, you can review the completed code for it in the .
Next Steps
More information
For more information about the topics covered in this lesson, visit: