In this section, we will create our first Angular application. This will help you understand the basic structure of an Angular project and how to run it. By the end of this section, you will have a working Angular application running on your local machine.
Steps to Create Your First Angular Application
- Install Angular CLI
Angular CLI (Command Line Interface) is a powerful tool that helps you initialize, develop, scaffold, and maintain Angular applications.
Command to install Angular CLI:
- Create a New Angular Project
Once Angular CLI is installed, you can create a new Angular project using the ng new
command.
Command to create a new Angular project:
Explanation:
ng new
: This command creates a new Angular project.my-first-angular-app
: This is the name of your project. You can choose any name you like.
- Navigate to the Project Directory
After creating the project, navigate to the project directory.
Command to navigate to the project directory:
- Serve the Application
To run the application, use the ng serve
command. This will compile the application and start a development server.
Command to serve the application:
Explanation:
ng serve
: This command compiles the application and starts a development server. By default, the server 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
When you create a new Angular project, several files and directories are generated. Here is a brief overview of the most important ones:
File/Directory | Description |
---|---|
src/ |
Contains the source code of the application. |
src/app/ |
Contains the main application module and component files. |
src/assets/ |
Contains static assets like images and stylesheets. |
src/environments/ |
Contains environment-specific configuration files. |
angular.json |
Configuration file for Angular CLI. |
package.json |
Contains metadata about the project and its dependencies. |
tsconfig.json |
Configuration file for TypeScript. |
Key Files in src/app/
File | Description |
---|---|
app.module.ts |
The root module of the application. |
app.component.ts |
The root component of the application. |
app.component.html |
The HTML template of the root component. |
app.component.css |
The CSS styles for the root component. |
Modifying the Default Template
Let's modify the default template to display a custom message.
- Open
src/app/app.component.html
. - Replace the existing content with the following code:
- Save the file and refresh the browser. You should see the updated message.
Practical Exercise
Exercise 1: Customize the Welcome Message
- Open
src/app/app.component.html
. - Add a new paragraph below the existing content with a custom message.
- Save the file and verify the changes in the browser.
Solution:
<h1>Welcome to My First Angular Application!</h1> <p>This is a simple Angular application.</p> <p>Angular is a powerful framework for building web applications.</p>
Exercise 2: Add a New Component
- Generate a new component using Angular CLI.
- Modify the new component to display a custom message.
- Include the new component in the root component template.
Steps:
-
Generate a new component:
ng generate component my-new-component
-
Modify the new component:
- Open
src/app/my-new-component/my-new-component.component.html
. - Add the following content:
<p>This is my new component!</p>
- Open
-
Include the new component in the root component template:
- Open
src/app/app.component.html
. - Add the following line:
<app-my-new-component></app-my-new-component>
- Open
-
Save all files and refresh the browser.
Solution:
<!-- src/app/app.component.html --> <h1>Welcome to My First Angular Application!</h1> <p>This is a simple Angular application.</p> <p>Angular is a powerful framework for building web applications.</p> <app-my-new-component></app-my-new-component>
Summary
In this section, you learned how to:
- Install Angular CLI.
- Create a new Angular project.
- Serve the application and view it in a browser.
- Understand the basic project structure.
- Modify the default template.
- Create and include a new component.
By completing these steps, you now have a basic understanding of how to set up and run an Angular application. In the next module, we will dive deeper into Angular components and learn how to create and manage them effectively.
Angular Course
Module 1: Introduction to Angular
- What is Angular?
- Setting Up the Development Environment
- Angular Architecture
- First Angular Application
Module 2: Angular Components
- Understanding Components
- Creating Components
- Component Templates
- Component Styles
- Component Interaction
Module 3: Data Binding and Directives
- Interpolation and Property Binding
- Event Binding
- Two-Way Data Binding
- Built-in Directives
- Custom Directives
Module 4: Services and Dependency Injection
Module 5: Routing and Navigation
Module 6: Forms in Angular
Module 7: HTTP Client and Observables
- Introduction to HTTP Client
- Making HTTP Requests
- Handling HTTP Responses
- Using Observables
- Error Handling
Module 8: State Management
- Introduction to State Management
- Using Services for State Management
- NgRx Store
- NgRx Effects
- NgRx Entity
Module 9: Testing in Angular
Module 10: Advanced Angular Concepts
- Angular Universal
- Performance Optimization
- Internationalization (i18n)
- Custom Pipes
- Angular Animations