- Lab
- Core Tech

Guided: Angular Routing and Navigation
Master Angular routing by building a simple product catalog application with navigation. You'll create routes for product listing and details pages, implement navigation links, pass product IDs through routes, and optimize performance with lazy loading. This hands-on lab uses Angular CLI automation to minimize setup time and maximize learning.

Path Info
Table of Contents
-
Challenge
Introduction
Introduction
Welcome to the Angular Routing and Navigation Code Lab. In this hands-on lab, you'll build a product catalog application that demonstrates Angular's powerful routing capabilities. You'll learn to implement navigation between views, pass data through routes, and optimize performance with lazy loading.
Background
Globomantics, a growing e-commerce platform, needs to transform its single-page product showcase into a full-featured catalog with navigation. As their product line has expanded from 20 to over 500 items, customers struggle to find specific products without proper navigation and routing.
As a front-end developer on the team, you've been tasked with implementing a robust routing system that allows customers to browse products, view details, and navigate seamlessly through the application. Your implementation will directly impact user experience and sales conversion rates.
Familiarizing with the Program Structure
The application is built with Angular CLI and uses Angular Router for navigation. It includes the following key files:
src/app/app-routing.module.ts
: The main routing configuration modulesrc/app/components/home/home.component.ts
: The landing page componentsrc/app/components/products/products.component.ts
: Displays the product catalogsrc/app/components/product-detail/product-detail.component.ts
: Shows individual product detailssrc/app/services/product.service.ts
: Manages product data
The application uses Angular CLI's development server. You can run the application using
npx ng serve
in the terminal. Navigate tohttp://localhost:4200
in your browser to see the running application. Initially, you'll see a basic Angular app that needs routing implementation to transform it into a functional product catalog.Important Note: The browser application will not compile successfully and load in the browser until you complete Task 4 (implementing the product service methods). This is intentional - the service methods need to be implemented for the app to run. Complete Tasks 1-4 first, then run
npx ng serve
to see your progress.As you complete each task, refresh your browser to test the new functionality.
Note: When you're ready, dive into the coding process. Use the validation feedback to guide you if you encounter any problems.
-
Challenge
Angular: Basic Routing Setup
Introduction to Angular Routing
Angular Router is a powerful library that enables navigation between different views in your application. It maps URL paths to components, allowing users to navigate through your app using browser controls and creating a true single-page application experience.
You'll work with the following key concepts:
- Routes: Configuration objects that define which component loads for a given URL.
- RouterOutlet: A placeholder directive where routed components are displayed.
- RouterLink: A directive that creates clickable navigation links
- Route Parameters: Dynamic segments in URLs such as
/products/123
Understanding these concepts is essential. Routing is the backbone of any multi-page Angular application. Without it, users can’t bookmark pages, use the browser’s back button, or share direct links to content.
Now that you've reviewed the basics of Angular routing fundamentals, you're ready to implement navigation in the Globomantics product catalog. ## Understanding Route Parameters
Route parameters allow you to pass dynamic data through the URL. They're essential for creating detail pages where the content depends on which item the user selected. For example,
/products/123
contains the parameter123
, which identifies the specific product to display.Angular uses a colon (
:
) syntax to define parameters in routes, likeproducts/:id
. When a user navigates to/products/5
, Angular extracts the value5
and makes it available to your component. This creates clean, bookmarkable URLs that users can share.Keep in mind that all route parameters are strings by default. If you're working with numeric IDs, you'll need to convert them to numbers. You can use the
ActivatedRoute
service to access these parameters in your components. -
Challenge
Product Catalog Implementation
Working with Services and Observables
Angular services provide data to components, and they typically return Observables—a powerful way to handle asynchronous data. Observables are like promises but can emit multiple values over time.
When a service method returns an Observable, you need to subscribe to it to access the data.
The pattern looks like this:
service.getData().subscribe(data => { // Use the data here });
In this lab, you'll use the RxJS
of()
function to create Observables from static product data. This simulates real HTTP requests while keeping things simple and focused. ## Displaying Dynamic Content with Angular DirectivesAngular provides powerful directives for displaying dynamic content in templates. In this lab, you'll focus on two key tools:
*ngFor
: Repeats an HTML element for each item in an array.- String interpolation (
{{ }}
): Displays component data in the template.
When you use
*ngFor
, you create a template variable that represents each item in the array. For example,*ngFor="let product of products"
creates aproduct
variable you can use within that element.Event binding with
(click)
allows you to call component methods when users interact with elements. Combined with*ngFor
, this creates interactive lists where each item can trigger different actions. -
Challenge
Dynamic Product Details
Extracting Route Parameters
When implementing detail pages, you need to extract parameters from the current route. Angular provides the
ActivatedRoute
service for this purpose. This service gives you access to information about the active route, including its parameters.Route parameters are provided as an Observable, which means they can change over time. In this lab, they remain static, but you will still need to subscribe to the parameters and extract the value you need.
Remember that route parameters are always strings, so when working with numeric IDs, you must convert them. The unary plus operator (
+
) is a concise way to convert strings to numbers in JavaScript. -
Challenge
Performance Optimization with Lazy Loading
Introduction to Lazy Loading
Lazy loading is a design pattern that delays loading of resources until they're actually needed. In Angular, this means loading feature modules only when users navigate to their routes. This significantly improves initial load time.
The syntax for lazy loading uses dynamic imports:
loadChildren: () => import('./path/to/module').then(m => m.ModuleName)
This creates a separate bundle for the module that's only downloaded when needed. For features that not all users will access (like an About page), this keeps the main bundle smaller and the app faster. ## Testing Navigation Behavior
Testing routing ensures your navigation works correctly as the application grows. Angular provides testing utilities that simulate navigation and verify the resulting URLs.
The key concepts for route testing:
fakeAsync
andtick()
: Control asynchronous operations in testsrouter.navigate()
: Trigger navigation programmaticallylocation.path()
: Check the current URL
These tests prevent common issues like broken links, incorrect parameters, or navigation loops.
-
Challenge
Conclusion
Conclusion
Congratulations on completing the Angular Routing and Navigation lab! You've successfully transformed a static application into a dynamic, navigable product catalog.
What You've Accomplished
Throughout this lab, you have:
- Configured Angular Router: Set up routes mapping URLs to components with proper redirects.
- Implemented Navigation: Created a working navigation menu with active state highlighting.
- Handled Route Parameters: Passed product IDs through routes and extracted them to display dynamic content.
- Built a Product Catalog: Created a complete browsing experience from listing to details.
- Optimized with Lazy Loading: Implemented on-demand module loading for better performance.
- Tested Route Behavior: Wrote tests to ensure navigation works correctly.
Key Takeaways
The most important lessons from this lab are:
- Route Order Matters: Specific routes must come before general ones to match correctly.
- Parameters Enable Dynamic Content: Route parameters create reusable components for different data.
- Observables Handle Async Data: Subscribe to observables to get data from services.
- Lazy Loading Improves Performance: Load features on-demand to reduce initial bundle size.
- Testing Prevents Regressions: Route tests ensure navigation continues working as the app evolves.
Testing Your Complete Application
Before finishing, verify your implementation works correctly:
- Home Page: Navigate to http://localhost:4200 - should display the contents of the home page.
- Navigation: All links should work and highlight when active
- Products Grid: Should display 5 product cards with proper styling
- Product Details: Clicking any product should show its details at /products/{id}
- Back to Products Button: Should return to the products list
- About Page: Should load when first accessed (check Network tab for separate bundle)
- Direct URLs: Try accessing http://localhost:4200/products directly
Extending Your Skills
To continue improving your Angular routing expertise, consider exploring:
- Route guards for authentication and authorization
- Resolver services to pre-fetch data before navigation
- Query parameters for filtering and sorting
- Route animations for smooth transitions
- Nested routes for complex layouts
Remember that routing is fundamental to creating professional Angular applications. The patterns you've learned here—from basic navigation to lazy loading—provide the foundation for building scalable single-page applications that deliver excellent user experiences.
What's a lab?
Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.
Provided environment for hands-on practice
We will provide the credentials and environment necessary for you to practice right within your browser.
Guided walkthrough
Follow along with the author’s guided walkthrough and build something new in your provided environment!
Did you know?
On average, you retain 75% more of your learning if you get time for practice.