Hone logo
Hone
Problems

Angular Route Parameter Navigation and Display

This challenge focuses on implementing route parameters in an Angular application. Route parameters allow you to pass data within the URL when navigating between components, enabling dynamic content display and targeted actions. Successfully completing this challenge demonstrates a fundamental understanding of Angular routing and data handling.

Problem Description

You are tasked with creating an Angular application with two components: ProductListComponent and ProductDetailComponent. The ProductListComponent displays a list of products. Each product in the list should have a link that navigates to the ProductDetailComponent, passing the product's ID as a route parameter. The ProductDetailComponent should then receive this ID and display the details of the corresponding product.

Key Requirements:

  • Routing: Configure Angular routing to handle navigation to the ProductDetailComponent with a route parameter named id.
  • Parameter Passing: When a user clicks on a product link in ProductListComponent, the URL should update to include the product ID as a route parameter (e.g., /products/123).
  • Parameter Retrieval: The ProductDetailComponent should retrieve the id parameter from the current route.
  • Data Display: The ProductDetailComponent should use the retrieved id to fetch and display the details of the specific product. For simplicity, assume you have a hardcoded product data structure (see "Example" below).
  • Error Handling: If the id is invalid (e.g., not a number, or the product with that ID doesn't exist), display an appropriate error message in the ProductDetailComponent.

Expected Behavior:

  1. The application should start at ProductListComponent.
  2. Clicking a product link should navigate to ProductDetailComponent with the correct ID in the URL.
  3. ProductDetailComponent should display the details of the product matching the ID.
  4. If an invalid ID is provided, ProductDetailComponent should display an error message.

Edge Cases to Consider:

  • What happens if the user navigates directly to /products/:id without clicking a link?
  • What happens if the id parameter is not a number?
  • What happens if the product with the given id does not exist in the data?

Examples

Example 1:

Input: Product List: [ {id: 1, name: "Laptop"}, {id: 2, name: "Mouse"} ]
User clicks on the "Laptop" link (which generates a link to /products/1)
Output: ProductDetailComponent displays: {id: 1, name: "Laptop"}
Explanation: The route parameter 'id' is passed as 1, and ProductDetailComponent retrieves and displays the product with id 1.

Example 2:

Input: Product List: [ {id: 1, name: "Laptop"}, {id: 2, name: "Mouse"} ]
User navigates directly to /products/abc
Output: ProductDetailComponent displays: "Invalid Product ID"
Explanation: The route parameter 'id' is 'abc' (not a number). ProductDetailComponent handles this invalid input and displays an error message.

Example 3:

Input: Product List: [ {id: 1, name: "Laptop"}, {id: 2, name: "Mouse"} ]
User navigates to /products/3
Output: ProductDetailComponent displays: "Product not found"
Explanation: The route parameter 'id' is 3, but there is no product with id 3 in the data. ProductDetailComponent handles this case and displays an error message.

Constraints

  • Angular Version: Use Angular 16 or later.
  • Data Structure: Use a simple JavaScript array of product objects for demonstration purposes. Each product object should have an id (number) and a name (string) property.
  • Hardcoded Data: You can hardcode the product data within the application for simplicity.
  • Component Interaction: Focus on routing and parameter handling. No external API calls are required.
  • Performance: The solution should be efficient enough for a small dataset (less than 10 products).

Notes

  • Consider using the ActivatedRoute service to access the route parameters.
  • Use TypeScript for type safety.
  • Think about how to handle invalid input gracefully and provide informative error messages to the user.
  • Focus on the core functionality of route parameter handling; elaborate styling or complex UI elements are not required.
  • Remember to configure your app-routing.module.ts file correctly to define the routes.
Loading editor...
typescript