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.ts
file located in thehello
folder. This file defines the TypeScript code for the component.Replace the existing code in
hello.component.ts
with the following:
Open the
app.component.html
file located in thesrc/app
folder. This file is the main template file for the application.Replace the existing code in
app.component.html
with 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.ts
file located in thehello
folder.Update the code in
hello.component.ts
as follows:
In the
template
section of the component, we have added a paragraph (<p>
) element that displays thecourseName
using interpolation ({{ courseName }}
). Below that, we added a button element with an event binding(click)="changeCourseName()"
.Inside the component class, we declared the
courseName
variable 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 thecourseName
to the new value.Save the changes to the
hello.component.ts
file.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
courseName
displayed.Click the "Change Course Name" button, and you will see the
courseName
value 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.ts
file located in thecar-list
folder. This file defines the TypeScript code for the component.Replace the existing code in
car-list.component.ts
with the following:
Open the
app.component.html
file located in thesrc/app
folder. This file is the main template file for the application.Replace the existing code in
app.component.html
with 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
cars
array) changes.
Comments
Post a Comment