Angular Tutorial-Part 1
Angular Tutorial for Beginners – Complete Guide
Learn Angular step by step: setup, components, data binding, directives, services, routing, forms, HTTP and Signals – with ready-to-copy code examples.
1What is Angular?
Angular is a free, open-source front-end framework developed by Google for building fast, scalable single-page applications (SPAs). It uses TypeScript and gives you everything out of the box: components, routing, forms, HTTP client, testing tools and a powerful CLI.
| Feature | Benefit |
|---|---|
| Component-based | Reusable, testable UI blocks |
| TypeScript | Strong typing & better tooling |
| Dependency Injection | Clean, maintainable services |
| Angular CLI | Generate, build, test with one command |
| Signals | Fine-grained, fast reactivity |
2Installation & Setup
Install Node.js (LTS version) first, then install the Angular CLI globally:
npm install -g @angular/cli
ng version
# create a new project
ng new my-first-app
cd my-first-app
ng serve --openhttp://localhost:4200 and reloads automatically when you save a file.Important project files:
src/main.ts– entry point that bootstraps the appsrc/app/app.component.ts– root componentsrc/app/app.routes.ts– route definitionsangular.json– workspace/build configuration
3Components
A component = TypeScript class + HTML template + CSS styles. Generate one with the CLI:
ng generate component helloimport { Component } from '@angular/core';
@Component({
selector: 'app-hello',
standalone: true,
template: `<h2>Hello, {{ name }}!</h2>`,
styles: [`h2 { color: #dd0031; }`]
})
export class HelloComponent {
name = 'A143MK Readers';
}Use it in any template: <app-hello></app-hello>
4Data Binding
Angular supports four types of data binding. Click the tabs:
<p>Welcome {{ userName }}</p><img [src]="imageUrl" [alt]="title">
<button [disabled]="isLoading">Save</button><button (click)="sayHello()">Click Me</button><!-- import FormsModule in the component -->
<input [(ngModel)]="userName">
<p>Hello {{ userName }}</p>Hello, Guest 👋
5Control Flow & Directives
Modern Angular (v17+) uses the built-in control flow syntax @if, @for and @switch:
@if (isLoggedIn) {
<p>Welcome back!</p>
} @else {
<button (click)="login()">Login</button>
}
<ul>
@for (item of items; track item.id) {
<li>{{ item.name }}</li>
} @empty {
<li>No items found</li>
}
</ul>Attribute directives change appearance or behaviour:
<div [class.active]="isActive" [style.color]="textColor">Styled text</div>*ngIf and *ngFor still works, but the new control flow is faster and recommended.6Parent ↔ Child Communication
import { Component, input, output } from '@angular/core';
@Component({
selector: 'app-product',
standalone: true,
template: `
<h3>{{ title() }}</h3>
<button (click)="buy.emit(title())">Buy</button>
`
})
export class ProductComponent {
title = input.required<string>(); // data IN from parent
buy = output<string>(); // event OUT to parent
}<app-product [title]="'Laptop'" (buy)="onBuy($event)" />7Services & Dependency Injection
Services hold shared logic and data. Angular injects them wherever needed.
import { Injectable } from '@angular/core';
@Injectable({ providedIn: 'root' })
export class TodoService {
private todos: string[] = ['Learn Angular', 'Read A143MK blog'];
getAll() { return this.todos; }
add(todo: string) { this.todos.push(todo); }
}import { Component, inject } from '@angular/core';
import { TodoService } from './todo.service';
@Component({ selector: 'app-todos', standalone: true, template: `...` })
export class TodosComponent {
private todoService = inject(TodoService);
todos = this.todoService.getAll();
}8Routing
import { Routes } from '@angular/router';
import { HomeComponent } from './home/home.component';
import { AboutComponent } from './about/about.component';
export const routes: Routes = [
{ path: '', component: HomeComponent },
{ path: 'about', component: AboutComponent },
{ path: 'user/:id', loadComponent: () =>
import('./user/user.component').then(m => m.UserComponent) },
{ path: '**', redirectTo: '' }
];<nav>
<a routerLink="/" routerLinkActive="active">Home</a>
<a routerLink="/about">About</a>
</nav>
<router-outlet />loadComponent enables lazy loading – the code is downloaded only when the route is visited.9Forms (Reactive Forms)
import { Component, inject } from '@angular/core';
import { FormBuilder, ReactiveFormsModule, Validators } from '@angular/forms';
@Component({
selector: 'app-login',
standalone: true,
imports: [ReactiveFormsModule],
template: `
<form [formGroup]="form" (ngSubmit)="submit()">
<input formControlName="email" placeholder="Email">
<input formControlName="password" type="password">
<button [disabled]="form.invalid">Login</button>
</form>
`
})
export class LoginComponent {
private fb = inject(FormBuilder);
form = this.fb.group({
email: ['', [Validators.required, Validators.email]],
password: ['', [Validators.required, Validators.minLength(6)]]
});
submit() { console.log(this.form.value); }
}10HTTP Client (Calling APIs)
import { ApplicationConfig } from '@angular/core';
import { provideRouter } from '@angular/router';
import { provideHttpClient } from '@angular/common/http';
import { routes } from './app.routes';
export const appConfig: ApplicationConfig = {
providers: [provideRouter(routes), provideHttpClient()]
};import { Injectable, inject } from '@angular/core';
import { HttpClient } from '@angular/common/http';
export interface Post { id: number; title: string; }
@Injectable({ providedIn: 'root' })
export class PostService {
private http = inject(HttpClient);
getPosts() {
return this.http.get<Post[]>('https://jsonplaceholder.typicode.com/posts');
}
}11Signals (Modern Reactivity)
import { Component, signal, computed, effect } from '@angular/core';
@Component({
selector: 'app-counter',
standalone: true,
template: `
<p>Count: {{ count() }} | Double: {{ double() }}</p>
<button (click)="increment()">+1</button>
`
})
export class CounterComponent {
count = signal(0);
double = computed(() => this.count() * 2);
constructor() {
effect(() => console.log('Count changed:', this.count()));
}
increment() { this.count.update(c => c + 1); }
}Count: 0 | Double: 0
12Useful Angular CLI Commands
| Command | Purpose |
|---|---|
ng new app-name | Create new project |
ng serve | Run dev server |
ng g c name | Generate component |
ng g s name | Generate service |
ng build | Production build (dist/ folder) |
ng test | Run unit tests |
ng update | Upgrade Angular version |
13🎯 Quick Quiz – Test Yourself
🎉 Congratulations! You've learned the core concepts of Angular.
If this tutorial helped you, share it and leave a comment below. — A143MK
https://a143mk.blogspot.com
ReplyDeletehttps://react-bootstrap.netlify.app/docs/getting-started/introduction
ReplyDeletehttps://react.dev/learn/creating-a-react-app
ReplyDelete