In this tutorial, we aim to introduce you to Angular, a robust JavaScript framework used for building dynamic web applications. By the end of this tutorial, you'll have an understanding of Angular's core concepts, and you'll set up your first Angular application.
You will learn:
- What Angular is and its main features.
- How to set up your development environment.
- Angular's key concepts, such as components, modules, and services.
- How to create a basic Angular application.
Prerequisites:
- Basic knowledge of HTML, CSS, and JavaScript.
- Familiarity with TypeScript is helpful but not mandatory.
First, you need to install Node.js and npm (Node Package Manager) in your system. You can download them from the official website.
Once installed, you can install Angular CLI (Command Line Interface) globally using npm:
npm install -g @angular/cli
To create a new Angular project, you can use Angular CLI command:
ng new my-first-app
This command creates a new directory named my-first-app
and sets up the initial Angular application.
Angular's primary building blocks are components and modules.
Components: These are the basic building blocks of an Angular application. A component controls a part of the UI (User Interface) that you see in the browser.
Modules: Angular apps are modular. An Angular module, or NgModule
, is a container for a cohesive block of code dedicated to an application domain, a workflow, or a closely related set of capabilities.
To run your app, navigate to your project directory and run:
cd my-first-app
ng serve
This will start a development server. You can view your application by opening http://localhost:4200/
in your browser.
Let's create a new component named hello-world
:
ng generate component hello-world
This will generate a new component with four files: hello-world.component.ts
, hello-world.component.html
, hello-world.component.css
, and hello-world.component.spec.ts
.
Here's what hello-world.component.ts
might look like:
import { Component } from '@angular/core';
@Component({
selector: 'app-hello-world',
templateUrl: './hello-world.component.html',
styleUrls: ['./hello-world.component.css']
})
export class HelloWorldComponent {
title = 'Hello World!';
}
In this code, we're creating a new component named HelloWorldComponent
and setting its title property to 'Hello World!'.
In this tutorial, you have learned what Angular is, how to set up your development environment, the basics of Angular components and modules, and how to create a simple Angular application.
For further learning, you can explore more about Angular's Directives, Services, Dependency Injection, and Routing.
ng generate component <component-name>
to create a new component.app.component.html
.ng generate module <module-name>
to create a new module.declarations
array.(click)
event to call your method.