- Lab
-
Libraries: If you want this lab, consider one of these libraries.
- Core Tech
Guided: Building a Storefront in Angular
This hands-on lab is designed to help you practice your understanding of Angular concepts such as Routing, Binding, and Styling. A series of guided steps will give you a walkthrough on how to apply your knowledge of these fundamental Angular concepts into an actual application. By the end of the lab, you will have demonstrated your ability to build a functioning Angular application using these concepts.
Lab Info
Table of Contents
-
Challenge
Introduction
In this Angular guided lab, you have been given a demo app called Globoticket. Launching this app will display a storefront containing multiple "bookable" events. Your task in this lab will be to implement an event details component that will display information for each of these events.
The guided steps in this lab will review topics such as using the Angular CLI to generate components, dependency injection, routing, styling, and data binding. Tasks can be validated by clicking on the checkboxes next to them. If you are stuck or would like to compare solutions, a
solutiondirectory has been provided for you as well.The app will not run until you have completed the Using the Angular CLI step. Afterwards, you can launch the app at anytime during the lab by doing the following;
- Click the Run button at the bottom right of the Terminal.
- Then view the web page by clicking this link to the Angular website: {{localhost:4200}}
- You can also view the webpage by clicking the Web Browser tab's Refresh button, once the app is running. It may take a moment for the page to load.
-
Challenge
Using the Angular CLI
First, you will need to generate a new component for your event details page. This can be trivially done through an Angular command known as
ng generate. This command can generate multiple templates of Angular files, but in this lab you will be using it to generate a component with theng generate componentcommand. You can also use abbreviated commands such asng g cinstead of the aforementioned command. Afterwards, you will need to perform dependency injection with anEventServicethat has already been created (and imported) as it provides important data and functionality for your component. Once yourEventServicedependency has been injected, you will need to use it to initialize two properties for yourEventDetailComponentclass using thegetEvents()andcreateNewEvent()methods provided by the service. You will need to use thethiskeyword for the instance of yourEventService. -
Challenge
Adding Routing
So far you've used the Angular CLI to generate a new component and performed dependency injection with an
EventServiceto provide data and functionality to yourevent-detailcomponent. Now you will need to add routing, which will navigate and send data to your component whenever an event is clicked from the home page.Within the
importssection of theapp.module.tsfile, you will see two paths already defined in theRouterModule. You will need to define another path for yourevent-detailcomponent. Remember that routing paths are matched first, so it would be a good idea to put your new path above the other two paths. Now you will need to add routing to the home page so that each event will navigate to your component when clicked. Within thehomedirectory of yoursrc/appshould be ahome.component.htmlfile. In this file there are multipleatags with an unspecifiedhreflink. You will need to replace each of thesehrefs with the[routerLink]directive. Lastly, you will need to add logic within yourEventDetailComponentto handle the incoming id parameter and find the corresponding event from youreventList. To do this, you will need to perform dependency injection again with the importedActivatedRouteandRouter. TheActivatedRoutecan be used to retrieve theidparameter using itssnapshot.paramMap.get('id')call. Thisidshould be used to find a matching event from youreventList(for example, by using thefilter()function). TheRoutercan be used to navigate back to the home page with thenavigate(['/'])call if an invalid id is given. LikeEventService, you will need to usethisfor all of these calls. -
Challenge
Understanding Styling
There are many ways to style html and css selectors in Angular. A popular method, and one that Angular bootstraps by default when you generate a component, is the usage of
templateUrlandstyleUrls. These properties that are defined within your component specify paths to external resources containing the html templates and css stylings to be used by your component. This helps keep your code modular and clean.When you generated your
event-detailcomponent, the components html and css files are initially empty. Running themoveTemplatesscript from before has given you a baseline template and stylings for your component which you will modify in this step and the next.If you launch the app and click any event, you should now be able to route to your
event-detailcomponent thanks to the routing from the previous step. Don't be surprised that the page is blank, as we still need to implement data binding. For now, you will need to add css stylings to your html template to align theback to homebutton. -
Challenge
Binding Data
You're in the home stretch, now! If you have launched the app as mentioned in the last step, you will have noticed that your
event-detailcomponent displays a blank page beside aback to homeandplace orderbutton. Even though your component has logic that retrieves a specified event from the parameters sent to it, you have not bound the retrieved data to the html template.In this final step, you will perform both one-way (using interpolation) and two-way binding for retrieved data, which will then be displayed in your
event-detailcomponent.Navigate back to the
event-detail.component.htmlfile. Near the start you will notice a bunch of fairly self-explanatory comments starting withAdd imageUrland ending withShow event price. All of these are in fact properties of theselectedEventretrieved in your component. If you want to see the properties in depth, you can check theevent/eventInfo.tsfile to see the interface forEventInfo. Now there should only be two pieces of data left to bind as denoted by the comments. The first one will require two-way binding, which is done by using the[(ngModel)]directive. By binding thenumTicketsproperty of yourselectedEventto theselectelement, changing the number of tickets in the dropdown menu will change the value of thenumTicketsproperty. Then you can use interpolation to display the total order price as the product ofnumTicketswith theprice, which will dynamically change depending on the number of tickets selected. All done! Now by launching your app and clicking each event you should be redirected to yourevent-detailcomponent which should display all the necessary event information.
About the author
Real skill practice before real-world application
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.
Learn by doing
Engage hands-on with the tools and technologies you’re learning. You pick the skill, we provide the credentials and environment.
Follow your guide
All labs have detailed instructions and objectives, guiding you through the learning process and ensuring you understand every step.
Turn time into mastery
On average, you retain 75% more of your learning if you take time to practice. Hands-on labs set you up for success to make those skills stick.