In this section, we will guide you through creating your first Angular application. By the end of this module, you will have a basic understanding of how to set up and run an Angular app.
Prerequisites
Before starting, ensure you have:
- Node.js and npm installed on your machine.
- Angular CLI installed globally (
npm install -g @angular/cli
).
Step-by-Step Guide
- Create a New Angular Project
First, open your terminal or command prompt and run the following command to create a new Angular project:
- Explanation:
ng new
: This command creates a new Angular workspace and an initial Angular app.my-first-app
: This is the name of your project. You can choose any name you prefer.
- Navigate to Your Project Directory
Once the project is created, navigate to the project directory:
- Serve the Application
To run your application, use the following command:
- Explanation:
ng serve
: This command builds the application and starts a web server. By default, it runs onhttp://localhost:4200
.
- Open the Application in a Browser
Open your web browser and navigate to http://localhost:4200
. You should see the default Angular welcome page.
Understanding the Project Structure
Here is a brief overview of the key files and directories in your new Angular project:
- e2e/: End-to-end tests for the application.
- node_modules/: Contains all the npm packages installed for the project.
- src/: The source code of your application.
- app/: Contains the main application code.
- app.component.ts: The main component of the application.
- app.module.ts: The root module of the application.
- assets/: Contains static assets like images and styles.
- environments/: Contains environment-specific configurations.
- app/: Contains the main application code.
- angular.json: Configuration file for Angular CLI.
- package.json: Lists the npm packages and scripts for the project.
- tsconfig.json: TypeScript configuration file.
Modifying the App Component
Let's make a simple modification to the default app component to understand how Angular components work.
- Open
app.component.html
app.component.html
Navigate to src/app/app.component.html
and replace its content with:
- Save and View Changes
Save the file and go back to your browser. You should see the updated content:
Practical Exercise
Exercise 1: Modify the App Component
- Open
src/app/app.component.ts
. - Change the
title
property to"My First Angular Application"
. - Update
src/app/app.component.html
to display thetitle
property using Angular interpolation.
Solution:
- Open
src/app/app.component.ts
and modify thetitle
property:
- Update
src/app/app.component.html
to use thetitle
property:
Common Mistakes and Tips
- Mistake: Forgetting to save files after making changes.
- Tip: Always save your files before checking the browser for updates.
- Mistake: Not running
ng serve
in the project directory.- Tip: Ensure you are in the correct directory (
my-first-app
) before runningng serve
.
- Tip: Ensure you are in the correct directory (
Conclusion
Congratulations! You have successfully created and modified your first Angular application. You now have a basic understanding of the Angular project structure and how to make simple changes to components. In the next module, we will dive deeper into Angular architecture and explore how different parts of an Angular application work together.
Angular 2+ Course
Module 1: Introduction to Angular
Module 2: TypeScript Basics
- Introduction to TypeScript
- TypeScript Variables and Data Types
- Functions and Arrow Functions
- Classes and Interfaces