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
ProductDetailComponentwith a route parameter namedid. - 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
ProductDetailComponentshould retrieve theidparameter from the current route. - Data Display: The
ProductDetailComponentshould use the retrievedidto 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
idis invalid (e.g., not a number, or the product with that ID doesn't exist), display an appropriate error message in theProductDetailComponent.
Expected Behavior:
- The application should start at
ProductListComponent. - Clicking a product link should navigate to
ProductDetailComponentwith the correct ID in the URL. ProductDetailComponentshould display the details of the product matching the ID.- If an invalid ID is provided,
ProductDetailComponentshould display an error message.
Edge Cases to Consider:
- What happens if the user navigates directly to
/products/:idwithout clicking a link? - What happens if the
idparameter is not a number? - What happens if the product with the given
iddoes 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 aname(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
ActivatedRouteservice 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.tsfile correctly to define the routes.