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

Injecting Dependencies

Angular allows a component or service to receive the objects it needs through Dependency Injection. Instead of creating those objects manually, Angular provides them through its injector.

What Does Injecting a Dependency Mean?

A dependency is an object required by another class to perform some operation.

Injecting a dependency means asking Angular to provide that object instead of creating the object directly with new.

Simple Definition

Dependency Injection allows a class to receive the object it needs from Angular rather than creating that object itself.

Dependency Injection Flow

Component Needs service
→
Constructor Declares dependency
→
Injector Resolves dependency
→
Service Instance Provided to component

Common Dependency Injection Approaches

Constructor Injection

Declare the required dependency in the constructor.

constructor(...)
inject()

Obtain a dependency using Angular's inject function.

inject()
Provider

Defines how Angular should provide a dependency.

providers
Injector Hierarchy

Angular resolves dependencies through its injector hierarchy.

Injector

1. Constructor Injection

Constructor injection is a common approach where a service is declared as a constructor parameter.

Service
import { Injectable } from '@angular/core';

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

    getCourseName(): string {

        return 'Angular Full Stack';

    }

}
Component
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 sees that CourseComponent requires CourseService and asks the injector to resolve that dependency.

2. Using the Injected Dependency

Once a service has been injected, the component can call its methods through the injected property.

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. Injecting Multiple Dependencies

A component can request multiple services when it needs functionality from different parts of the application.

Component
constructor(

    private courseService: CourseService,

    private trainingService: TrainingService,

    private notificationService:
        NotificationService

) {

}
CourseService Course Data
TrainingService Training Data
NotificationService Messages
→
Dashboard Uses Dependencies

4. Using inject()

Angular also provides the inject() function for obtaining dependencies in supported injection contexts.

Component
import { Component, inject } from '@angular/core';
import { CourseService } from './course.service';

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

    private courseService =
        inject(CourseService);

    courseName = '';

    loadCourse(): void {

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

    }

}
Important

The inject() function retrieves a dependency from Angular's dependency injection system without declaring it as a constructor parameter.

5. Constructor Injection vs inject()

Constructor Injection inject()
Dependency is declared as a constructor parameter. Dependency is obtained using the inject() function.
Common and easy to understand. Useful in supported Angular injection contexts.
Works naturally with class dependencies. Can reduce constructor parameters.
Example: constructor(private service: Service) Example: private service = inject(Service)

6. Dependency and Provider

Angular needs to know how a dependency should be provided. This information is configured through providers or service metadata.

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

}
Component Provider
@Component({

    selector: 'app-dashboard',

    providers: [
        TrainingService
    ],

    templateUrl: './dashboard.component.html'

})
export class DashboardComponent {

}
Key Idea

The provider determines how Angular can resolve the requested dependency in the appropriate injector hierarchy.

7. How Angular Resolves a Dependency

When a component requests a dependency, Angular looks for a matching provider in its injector hierarchy.

Resolution Flow
Component
    |
    ↓
Requests CourseService
    |
    ↓
Current Injector
    |
    ↓
Provider Found?
    |
    ├── Yes → Create / Reuse Service
    |
    └── No  → Look in Parent Injector
                    |
                    ↓
             Continue Hierarchy
Simple Idea

Angular follows its injector hierarchy to find a provider that can satisfy the dependency request.

8. Using an Injected Service in ngOnInit()

After a service has been injected, it can be used during component initialization to load or prepare data.

Component
courses: string[] = [];

constructor(
    private courseService: CourseService
) {

}

ngOnInit(): void {

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

}
Expected Behavior:

The injected service provides the course data during component initialization.

9. Injecting a Service into Another Service

Angular services can also have dependencies on other services.

notification.service.ts
@Injectable({
    providedIn: 'root'
})
export class NotificationService {

    show(message: string): void {

        console.log(message);

    }

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

    constructor(
        private notificationService:
            NotificationService
    ) {

    }

    saveCourse(): void {

        this.notificationService.show(
            'Course saved successfully'
        );

    }

}
Important

Dependency Injection is not limited to components. Services can also receive dependencies through Angular's injector.

10. Dependency Through an Abstraction

Angular can also use injection tokens when the dependency is represented by an abstraction rather than a concrete class.

Concept Example
export const API_URL =
    new InjectionToken<string>('API_URL');
Why Use an Injection Token?

Injection tokens are useful when Angular needs to inject a value or dependency that cannot be represented directly by a class type.

Complete CIIT Training Institute Example

Suppose the CIIT Training Institute dashboard needs course information and notifications. Both services can be injected into the dashboard.

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

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

    getCourses(): string[] {

        return [

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

        ];

    }

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

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

    show(message: string): void {

        console.log(message);

    }

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

import { CourseService }
from './course.service';

import { NotificationService }
from './notification.service';

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

    courses: string[] = [];

    constructor(

        private courseService:
            CourseService,

        private notificationService:
            NotificationService

    ) {

    }

    ngOnInit(): void {

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

        this.notificationService.show(
            'CIIT Dashboard Loaded'
        );

    }

}
dashboard.component.html
<h2>
    CIIT Training Institute
</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

Notification: CIIT Dashboard Loaded

Dependency Injection Quick Reference

Concept Purpose Example
Dependency Object required by another class CourseService
Constructor Injection Receive dependency through constructor constructor(...)
inject() Obtain dependency through Angular's inject function inject(Service)
Provider Defines how dependency is provided providedIn
Injector Resolves dependencies Injector

Important Points About Injecting Dependencies

  • A dependency is an object required by another class.
  • Angular can provide dependencies through its injector.
  • Constructor injection is a common way to receive services.
  • The inject() function can obtain dependencies in supported injection contexts.
  • Providers tell Angular how dependencies should be supplied.
  • Services can themselves have dependencies.

Dependency Injection Best Practices

  • Prefer Angular's Dependency Injection system instead of manually creating service objects.
  • Keep constructor dependencies meaningful and focused.
  • Use services for reusable application logic.
  • Choose an appropriate provider scope for each dependency.
  • Avoid injecting unnecessary services into a component.
  • Use injection tokens when a value or abstraction requires one.

Summary

Angular Dependency Injection allows a component or service to receive the objects it needs from Angular's injector.

Dependencies can be injected using constructor injection or inject(), while providers define how Angular resolves those dependencies.