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

Creating Angular Services

Angular services are reusable TypeScript classes that can be created to organize application logic, manage data, communicate with APIs, and provide common functionality to components.

What Does Creating a Service Mean?

Creating an Angular service means defining a reusable TypeScript class and registering it with Angular's dependency injection system.

Once created, the service can be injected into components and other Angular classes wherever its functionality is required.

Simple Definition

A service is a reusable class that contains functionality which can be shared across different parts of an Angular application.

Service Creation Flow

Angular CLI Generate service
→
Service Class Write reusable logic
→
Dependency Injection Provide service
→
Component Use the service

1. Create a Service Using Angular CLI

Angular CLI provides a command for generating a service quickly inside an Angular project.

Angular CLI Command
ng generate service course

The shorter version of the same command is:

Short Command
ng g service course
Result

Angular CLI creates the service file and prepares the class for dependency injection.

2. Files Generated by CLI

When a service is generated, Angular CLI normally creates a TypeScript service file.

Example Project Structure
src/
└── app/
    └── course.service.ts
course.service.ts

Contains the service class and its methods.

Service Class
TypeScript

Service logic is written in TypeScript.

class
Injectable

Angular can provide the service through dependency injection.

@Injectable()
Reusable

The service can be used by multiple components.

Reuse

3. Basic Service Structure

A basic Angular service contains the @Injectable() decorator and a TypeScript class.

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

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

}
What is @Injectable?

The @Injectable() decorator tells Angular that the class can participate in dependency injection.

4. Adding Methods to a Service

After creating a service, reusable methods can be added to provide functionality to components.

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

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

    getCourseName(): string {

        return 'Angular Full Stack';

    }

    getDuration(): string {

        return '5 Months';

    }

}
Service Methods:

Course: Angular Full Stack

Duration: 5 Months

5. Using the Created Service in a Component

After creating a service, it can be injected into a component through the constructor.

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
    class="btn btn-primary"
    (click)="loadCourse()">

    Load Course

</button>

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

Course: Angular Full Stack

6. Creating a Service with Data

A service can store application data and expose methods for accessing that data.

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;

    }

}
Why Keep Data in the Service?

Multiple components can use the same service method instead of storing duplicate course data in every component.

7. Useful Service Creation Commands

Command Purpose
ng generate service course Creates a service named CourseService.
ng g service course Short form of the generate service command.
ng generate service services/course Creates a service inside a services folder.

8. Organizing Services in a Project

In a larger Angular application, services can be organized inside a dedicated folder.

Suggested Structure
src/
└── app/
    ├── components/
    ├── services/
    │   ├── course.service.ts
    │   ├── training.service.ts
    │   └── notification.service.ts
    └── app.component.ts
Benefit

Keeping related services organized makes the project easier to navigate and maintain.

9. providedIn: 'root'

A commonly used service configuration is providedIn: 'root'.

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

    getInstituteName(): string {

        return 'CIIT Training Institute';

    }

}
Service TrainingService
→
Root Provider Application Scope
→
Components Can Inject Service

Complete CIIT Training Institute Example

Let's create a service for CIIT Training Institute that provides the institute name and available courses.

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'

    ];

    getInstituteName(): string {

        return 'CIIT Training Institute';

    }

    getCourses(): string[] {

        return this.courses;

    }

}
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

Service Creation Checklist

  • Generate the service using Angular CLI.
  • Create meaningful methods inside the service.
  • Use @Injectable() for dependency injection.
  • Use providedIn: 'root' when an application-wide service is appropriate.
  • Inject the service into components that need it.
  • Keep reusable logic inside services instead of duplicating it across components.

Creating Services Best Practices

  • Use descriptive service names such as CourseService or TrainingService.
  • Keep one service focused on a related responsibility.
  • Keep API communication and reusable data logic inside services.
  • Avoid placing unnecessary UI logic inside services.
  • Organize services into folders in larger projects.
  • Use dependency injection instead of manually creating service instances.

Summary

Angular services can be created using the Angular CLI and implemented as reusable TypeScript classes with the @Injectable() decorator.

After creation, services can expose methods and data that components can access through Angular's dependency injection system.