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

Using Angular Services

Angular Services can be injected into components and other Angular classes to provide reusable functionality such as data access, business logic, API communication, and shared application state.

What Does Using a Service Mean?

After creating a service, a component can request that service through Angular's dependency injection system.

The component can then call service methods and use the data or functionality provided by the service.

Simple Definition

Using a service means injecting the service into a component and calling its methods whenever the component needs its functionality.

Using a Service Flow

Service Reusable functionality
→
Dependency Injection Provides service
→
Component Uses service
→
Template Displays result

1. Injecting a Service into a Component

A service can be injected into a component constructor. Angular's dependency injection system provides the service instance.

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

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

    getCourseName(): string {

        return 'Angular Full Stack';

    }

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

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

    constructor(
        private courseService: CourseService
    ) {

    }

}
How it works

Angular detects that the component requires CourseService and provides its available service instance through dependency injection.

2. Calling a Service Method

After injecting the service, the component can call the methods provided by that service.

Component
courseName = '';

constructor(
    private courseService: CourseService
) {

}

loadCourse(): void {

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

}
Template
<button
    class="btn btn-primary"
    (click)="loadCourse()">

    Load Course

</button>

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

Course: Angular Full Stack

3. Using Multiple Service Methods

A component can use multiple methods from the same service.

training.service.ts
@Injectable({
    providedIn: 'root'
})
export class TrainingService {

    getCourseName(): string {

        return 'Python Full Stack';

    }

    getDuration(): string {

        return '5 Months';

    }

    getMode(): string {

        return 'Online + Offline';

    }

}
Component
courseName = '';

duration = '';

mode = '';

constructor(
    private trainingService: TrainingService
) {

}

loadCourseDetails(): void {

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

    this.duration =
        this.trainingService.getDuration();

    this.mode =
        this.trainingService.getMode();

}
Expected Output:

Course: Python Full Stack

Duration: 5 Months

Mode: Online + Offline

4. Using a Service in ngOnInit()

A service is often used during component initialization to load initial application data.

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

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

    courses: string[] = [];

    constructor(
        private trainingService: TrainingService
    ) {

    }

    ngOnInit(): void {

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

    }

}
Template
<h3>
    Available Courses
</h3>

<ul>

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

</ul>
Typical Pattern

The component receives the service through dependency injection and calls the service method from ngOnInit() to prepare the initial data.

5. Using a Service That Returns an Array

Services can return arrays that can be displayed by components.

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

    private courses: string[] = [

        'Java Full Stack',
        'Python Full Stack',
        'Full Stack .NET',
        'Data Analytics',
        'Data Science'

    ];

    getCourses(): string[] {

        return this.courses;

    }

}
Component
courses: string[] = [];

constructor(
    private courseService: CourseService
) {

}

ngOnInit(): void {

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

}
Expected Output:
  • Java Full Stack
  • Python Full Stack
  • Full Stack .NET
  • Data Analytics
  • Data Science

6. Passing Parameters to Service Methods

Service methods can accept parameters when a component needs dynamic functionality.

course.service.ts
getCourseMessage(courseName: string): string {

    return 'Selected Course: ' + courseName;

}
Component
message = '';

constructor(
    private courseService: CourseService
) {

}

selectCourse(course: string): void {

    this.message =
        this.courseService
            .getCourseMessage(course);

}
Template
<button
    class="btn btn-primary"
    (click)="selectCourse('Full Stack .NET')">

    Select Course

</button>

<p>
    {{ message }}
</p>
Expected Output:
Selected Course: Full Stack .NET

7. Using an API Service

A service can use HttpClient to communicate with a backend API. Components can then subscribe to the returned Observable.

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
courses: any[] = [];

constructor(
    private courseService: CourseService
) {

}

ngOnInit(): void {

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

            this.courses = data;

        });

}
Component Requests Data
→
Service Calls API
→
Backend API Returns Data
→
Component Displays Data

8. Using the Same Service in Multiple Components

A service can be injected into more than one component when those components need the same functionality.

CourseService Shared functionality
→
CourseList Uses service
→
Dashboard Uses service
→
Notification Uses service

9. Common Service Responsibilities

Get Data

Retrieve application data from local or remote sources.

getCourses()
Create Data

Send or prepare new application data.

addCourse()
Update Data

Update existing application information.

updateCourse()
Delete Data

Remove application data through reusable service methods.

deleteCourse()

Complete CIIT Training Institute Example

Suppose the CIIT Training Institute dashboard needs to display available courses and training modes. The component can obtain this information from a reusable service.

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

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

    getInstituteName(): string {

        return 'CIIT Training Institute';

    }

    getCourses(): string[] {

        return [
            'Java Full Stack',
            'Python Full Stack',
            'Full Stack .NET',
            'Data Analytics'
        ];

    }

    getTrainingModes(): string[] {

        return [
            'Online',
            'Offline'
        ];

    }

}
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[] = [];

    modes: string[] = [];

    constructor(
        private trainingService: TrainingService
    ) {

    }

    ngOnInit(): void {

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

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

        this.modes =
            this.trainingService.getTrainingModes();

    }

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

<h4>
    Available Courses
</h4>

<ul>

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

</ul>

<h4>
    Training Modes
</h4>

<ul>

    <li *ngFor="let mode of modes">
        {{ mode }}
    </li>

</ul>
Expected Output:

CIIT Training Institute

Available Courses

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

Training Modes

  • Online
  • Offline

Service Usage Checklist

  • Inject the required service into the component.
  • Store the injected service in a private property.
  • Call service methods instead of duplicating reusable logic.
  • Use ngOnInit() when initial service data needs to be loaded.
  • Subscribe to Observable results when using API-based services.
  • Keep UI-specific logic in the component and reusable logic in the service.

Using Services Best Practices

  • Inject services instead of creating service objects manually.
  • Keep service responsibilities focused and meaningful.
  • Do not duplicate the same business logic across components.
  • Keep API communication inside dedicated services.
  • Use meaningful method names such as getCourses() and getInstituteName().
  • Keep components simple by delegating reusable operations to services.

Summary

Angular Services are used by injecting them into components through Angular's dependency injection system.

Once injected, components can call service methods to access data, business logic, API functionality, and other reusable operations.