1. a. Angular Application Setup
Observe the link http://localhost:4200/welcome on which the mCart application is running. Perform the below activities to understand the features of the application.
Angular Application Setup:
Install Node.js: Visit the Node.js website (https://nodejs.org) and download the latest version of Node.js. Follow the installation instructions specific to your operating system.
Install Angular CLI: Open a terminal or command prompt and run the following command to install the Angular CLI globally:
To create a new component called "hello" and render "Hello Angular" on the page in an Angular application, follow these steps:
Open a terminal or command prompt.
Navigate to the root directory of your Angular project.
Run the following command to generate the new component:
This command creates a new folder called "hello" with the necessary files for the component.
Open the
hello.component.tsfile located in thehellofolder. This file defines the TypeScript code for the component.Replace the existing code in
hello.component.tswith the following:
Open the
app.component.htmlfile located in thesrc/appfolder. This file is the main template file for the application.Replace the existing code in
app.component.htmlwith the following:
Save the changes to both files.
Run the Angular development server if it's not already running by executing the following command in the terminal:
To add an event to the hello component template in Angular, which changes the courseName when clicked, follow these steps:
Open the
hello.component.tsfile located in thehellofolder.Update the code in
hello.component.tsas follows:
In the
templatesection of the component, we have added a paragraph (<p>) element that displays thecourseNameusing interpolation ({{ courseName }}). Below that, we added a button element with an event binding(click)="changeCourseName()".Inside the component class, we declared the
courseNamevariable and initialized it with the initial course name.We also added the
changeCourseName()method, which is called when the button is clicked. Inside this method, we update thecourseNameto the new value.Save the changes to the
hello.component.tsfile.Run your Angular development server if it's not already running by executing the following command in the terminal:
Open a web browser and navigate to http://localhost:4200. You should see the "Hello Angular" message along with the initial
courseNamedisplayed.Click the "Change Course Name" button, and you will see the
courseNamevalue being updated to "Advanced Angular Concepts" on the page.
To progressively build the PoolCarz application and understand how change detection works in Angular, you can follow these steps:
- Create a new Angular project: Open a terminal or command prompt, navigate to the desired directory, and run the following command to create a new Angular project named "PoolCarz":
This command will create a new folder called "car-list" with the necessary files for the component.
Open the
car-list.component.tsfile located in thecar-listfolder. This file defines the TypeScript code for the component.Replace the existing code in
car-list.component.tswith the following:
Open the
app.component.htmlfile located in thesrc/appfolder. This file is the main template file for the application.Replace the existing code in
app.component.htmlwith the following:
Save the changes to both files.
Run the Angular development server if it's not already running by executing the following command in the terminal:
Open a web browser and navigate to http://localhost:4200. You should see the "PoolCarz Application" heading along with the "Available Cars" heading and the initial car list displayed.
Click the "Add Car" button, and you should see a new car added to the list without a full page reload. This demonstrates the concept of change detection in Angular, where the UI is automatically updated when the underlying data (the
carsarray) changes.
Comments
Post a Comment