Master Angular From Scratch

Learn Angular with clear, interactive, and structured lessons for beginners and developers.

ANGULAR COURSE SYLLABUS
1. Angular Introduction
Angular Introduction Angular History Angular Fundamentals Angular Architecture Component-Based Angular
2. Angular Setup
Angular Setup Introduction Installing Node.js Installing Angular CLI Creating an Angular Application Angular Project Structure
3. Components
Components Overview Component Anatomy Creating Components Component Selector Component Template Component Styles Component Metadata Standalone Components Parent and Child Components Reusable Components
4. Templates
Templates Introduction Template Syntax Template Expressions Template Statements Template Reference Variables ng-template ng-container ngTemplateOutlet Built-in Control Flow
5. Data Binding
Data Binding Introduction Interpolation Binding Property Binding Event Binding Two-Way Data Binding Class & Style Binding
6. Directives
Directives Introduction Attribute Directives Structural Directives Custom Directives
7. Pipes
Pipes Introduction Built-in Pipes Parameterized Pipes Chaining Pipes Custom Pipes
8. Event Handling
Event Handling Introduction DOM Events Event Binding Syntax Event Object Passing Data to Event Handlers
9. Input & Output
Component Communication @Input @Output EventEmitter
10. Lifecycle Hooks
Lifecycle Hooks Introduction ngOnInit ngOnChanges ngAfterViewInit ngOnDestroy
11. Services
Services Introduction Creating Angular Services Using Services in Components Sharing Data Through Services Service Scope
12. Dependency Injection
Dependency Injection Introduction Injecting Dependencies InjectionToken Provider Configuration
13. Routing
Routing Introduction Route Configuration RouterLink & Navigation Route Parameters Child Routes Route Guards
14. Forms
Angular Forms Introduction Template-Driven Forms Reactive Forms Form Validation Form Submission & Error Handling
15. HttpClient
HttpClient Introduction Configuring HttpClient GET Request POST, PUT & DELETE Requests Headers & Parameters HTTP Error Handling
16. Observables
Observables Introduction Creating & Subscribing to Observables Observable Data Streams Observable Operators Subscription Management
17. RxJS
RxJS Introduction RxJS Operators Transformation & Filtering Combining Observables Higher-Order Mapping Subjects
18. API Integration
API Integration Introduction Connecting Angular with REST API Consuming API Data CRUD API Integration API Response & Error Handling

Angular Services

Angular Services are reusable classes used to organize business logic, share data, perform API operations, and provide common functionality across components.

What is an Angular Service?

A service is a reusable TypeScript class that contains logic which can be shared by multiple Angular components.

Services help keep components simple by moving reusable logic, data operations, API calls, and other common functionality into separate classes.

Simple Definition

An Angular Service is a reusable class that provides functionality to one or more components.

Angular Service Flow

Component Requests functionality
→
Service Contains reusable logic
→
Data / API Processes information
→
Component Receives result

Why Use Angular Services?

Reusable Logic

Write functionality once and reuse it in multiple components.

Reusable Code
Share Data

Share application data between different components.

Shared Data
API Calls

Keep HTTP and API-related operations outside components.

HttpClient
Clean Components

Keep components focused on UI and user interaction.

Separation

Basic Service Structure

An Angular service is normally created as a TypeScript class and can be provided using Angular dependency injection.

course.service.ts
import { Injectable } from '@angular/core';

@Injectable({
    providedIn: 'root'
})
export class CourseService {

    getCourseName(): string {

        return 'Angular Full Stack';

    }

}
Important

The @Injectable() decorator marks the class as available for Angular's dependency injection system.

Component Using a Service

A component can receive a service through its constructor and then use the service methods.

course.component.ts
import { Component } from '@angular/core';
import { CourseService } from './course.service';

@Component({
    selector: 'app-course',
    templateUrl: './course.component.html'
})
export class CourseComponent {

    courseName = '';

    constructor(
        private courseService: CourseService
    ) {

    }

    loadCourse(): void {

        this.courseName =
            this.courseService.getCourseName();

    }

}
course.component.html
<button
    (click)="loadCourse()"
    class="btn btn-primary">

    Load Course

</button>

<p>
    Course:
    {{ courseName }}
</p>
Expected Output:

Course: Angular Full Stack

What Can a Service Do?

Data Management

Store and manage application-related data.

getCourses()
API Communication

Communicate with backend APIs using HttpClient.

http.get()
Business Logic

Keep reusable calculations and application rules.

calculateFee()
Shared State

Share information between components when required.

BehaviorSubject

1. Reusable Service Example

The same service can be injected into different components, allowing the application to reuse the same logic.

notification.service.ts
import { Injectable } from '@angular/core';

@Injectable({
    providedIn: 'root'
})
export class NotificationService {

    showMessage(message: string): void {

        console.log(message);

    }

}
dashboard.component.ts
constructor(
    private notificationService: NotificationService
) {

}

showWelcome(): void {

    this.notificationService.showMessage(
        'Welcome to Angular'
    );

}
How it works

The notification logic remains in the service, while the component simply calls the required service method.

2. Service for Course Data

A course service can centralize course-related data and allow multiple components to access the same information.

course.service.ts
@Injectable({
    providedIn: 'root'
})
export class CourseService {

    private courses: string[] = [
        'Java Full Stack',
        'Python Full Stack',
        'Full Stack .NET',
        'Data Analytics'
    ];

    getCourses(): string[] {

        return this.courses;

    }

}
Component
courses: string[] = [];

constructor(
    private courseService: CourseService
) {

}

ngOnInit(): void {

    this.courses =
        this.courseService.getCourses();

}
Expected Output:

Available Courses

  • Java Full Stack
  • Python Full Stack
  • Full Stack .NET
  • Data Analytics

3. Service for API Communication

API communication is commonly placed inside a service rather than directly inside a component.

course.service.ts
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';

@Injectable({
    providedIn: 'root'
})
export class CourseService {

    private apiUrl =
        'https://api.example.com/courses';

    constructor(
        private http: HttpClient
    ) {

    }

    getCourses() {

        return this.http.get(this.apiUrl);

    }

}
Component
ngOnInit(): void {

    this.courseService
        .getCourses()
        .subscribe(data => {

            this.courses = data;

        });

}
Component Requests Courses
→
CourseService Performs HTTP Request
→
API Returns Data
→
Component Displays Data

4. Sharing Data Between Components

A service can maintain shared data so that multiple components can access the same information.

shared-course.service.ts
import { Injectable } from '@angular/core';

@Injectable({
    providedIn: 'root'
})
export class SharedCourseService {

    selectedCourse = '';

    setCourse(course: string): void {

        this.selectedCourse = course;

    }

    getCourse(): string {

        return this.selectedCourse;

    }

}
Simple Idea

One component can update shared information through the service, while another component can read the same information from that service.

5. Service Scope

Angular services can be provided at different levels depending on how widely the service should be available.

Provider Scope Purpose
providedIn: 'root' Application-wide Shared service available throughout the application.
Component Provider Component-level Service instance associated with a component scope.
Module / Injector Scope Specific application scope Used when service availability needs to be restricted.

6. Dependency Injection with Services

Angular's dependency injection system creates and provides service instances to components that request them.

Example
constructor(
    private courseService: CourseService
) {

}
Service CourseService
→
Injector Resolves Dependency
→
Component Receives Service

Complete CIIT Training Institute Example

Suppose CIIT Training Institute has a course dashboard that needs to display available courses. Instead of keeping course data and related logic inside the component, we can place it in a service.

training.service.ts
import { Injectable } from '@angular/core';

@Injectable({
    providedIn: 'root'
})
export class TrainingService {

    private courses: string[] = [
        'Java Full Stack',
        'Python Full Stack',
        'Full Stack .NET',
        'Data Analytics'
    ];

    getCourses(): string[] {

        return this.courses;

    }

    getInstituteName(): string {

        return 'CIIT Training Institute';

    }

}
dashboard.component.ts
import { Component, OnInit } from '@angular/core';
import { TrainingService } from './training.service';

@Component({
    selector: 'app-dashboard',
    templateUrl: './dashboard.component.html'
})
export class DashboardComponent
    implements OnInit {

    instituteName = '';

    courses: string[] = [];

    constructor(
        private trainingService: TrainingService
    ) {

    }

    ngOnInit(): void {

        this.instituteName =
            this.trainingService.getInstituteName();

        this.courses =
            this.trainingService.getCourses();

    }

}
dashboard.component.html
<h2>
    {{ instituteName }}
</h2>

<h4>
    Available Courses
</h4>

<ul>

    <li *ngFor="let course of courses">
        {{ course }}
    </li>

</ul>
Expected Output:

CIIT Training Institute

Available Courses

  • Java Full Stack
  • Python Full Stack
  • Full Stack .NET
  • Data Analytics

Component vs Service

Component Service
Mainly manages UI and user interaction. Mainly manages reusable application logic.
Contains template-related behavior. Can contain data and business logic.
Uses services when functionality is needed. Can be injected into multiple components.
Should avoid unnecessary reusable logic. Helps keep reusable logic centralized.

Important Points About Angular Services

  • Services are reusable TypeScript classes.
  • Services help separate application logic from UI code.
  • Services can be used for data management and API communication.
  • Multiple components can use the same service.
  • Angular dependency injection provides required services.
  • Services can be used to share data between components.

Angular Service Best Practices

  • Keep reusable logic inside services instead of duplicating it across components.
  • Keep components focused mainly on presentation and interaction.
  • Use meaningful service names such as CourseService or TrainingService.
  • Keep API communication inside dedicated services.
  • Avoid putting unrelated responsibilities into one large service.
  • Use dependency injection instead of manually creating service instances.

Summary

Angular Services are reusable classes that help organize application logic, data operations, API communication, and shared functionality.

By using services with Angular dependency injection, applications become more reusable, maintainable, and easier to organize.