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 InjectionToken

Angular InjectionToken is used to create a unique token that can represent a value, configuration object, interface, or other dependency that cannot be identified directly by a class.

What is InjectionToken?

Angular dependency injection normally uses a class type to identify a dependency. However, some dependencies are values or abstractions that do not have a runtime class representation.

InjectionToken provides a unique token that Angular can use to identify and inject such dependencies.

Simple Definition

An InjectionToken is a unique key used by Angular's Dependency Injection system to identify a value or non-class dependency.

InjectionToken Flow

InjectionToken Creates unique token
→
Provider Associates value
→
Injector Resolves token
→
Component Receives value

Why Use InjectionToken?

Unique Identifier

Provides a unique identifier for a dependency.

new InjectionToken()
Configuration

Useful for injecting application configuration values.

API_URL
Non-Class Values

Can represent strings, objects, arrays, and other values.

string | object
Abstraction

Can represent dependencies that are not concrete classes.

Token

1. Basic InjectionToken Syntax

An InjectionToken can be created by importing InjectionToken from Angular core.

token.ts
import { InjectionToken } from '@angular/core';

export const API_URL =
    new InjectionToken<string>('API_URL');
Important

The generic type <string> describes the type of value associated with the token.

2. Providing a Value with InjectionToken

After creating a token, a provider can associate a value with it.

app.config.ts
import { ApplicationConfig } from '@angular/core';

import { API_URL } from './tokens';

export const appConfig: ApplicationConfig = {

    providers: [

        {
            provide: API_URL,
            useValue: 'https://api.example.com'
        }

    ]

};
API_URL InjectionToken
→
provide Associates Token
→
useValue Provides String

3. Injecting an InjectionToken

Once a value is registered against a token, it can be injected into a component.

Component
import {
    Component,
    Inject
} from '@angular/core';

import { API_URL } from './tokens';

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

    constructor(
        @Inject(API_URL)
        private apiUrl: string
    ) {

    }

}
How it works

Angular uses the API_URL token to locate the value registered by the provider and supplies that value to the component.

4. Injecting a Token with inject()

An InjectionToken can also be obtained using Angular's inject() function.

Component
import {
    Component,
    inject
} from '@angular/core';

import { API_URL } from './tokens';

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

    private apiUrl =
        inject(API_URL);

}
Injected Value:
https://api.example.com

5. InjectionToken for String Values

A token can represent a simple string configuration value.

tokens.ts
export const APP_NAME =
    new InjectionToken<string>('APP_NAME');
Provider
{
    provide: APP_NAME,
    useValue: 'CIIT Training Institute'
}
Expected Value:
CIIT Training Institute

6. InjectionToken for Configuration Objects

InjectionToken can also represent an object containing multiple configuration values.

app-config.ts
export interface AppConfig {

    apiUrl: string;

    environment: string;

    enableLogging: boolean;

}

export const APP_CONFIG =
    new InjectionToken<AppConfig>('APP_CONFIG');
Provider
{
    provide: APP_CONFIG,

    useValue: {

        apiUrl: 'https://api.example.com',

        environment: 'production',

        enableLogging: true

    }
}
Benefit

A single token can represent a structured configuration object instead of creating separate dependencies for every setting.

7. InjectionToken with Factory Provider

A factory provider can calculate or create the value associated with an InjectionToken.

Provider
export const CURRENT_ENVIRONMENT =
    new InjectionToken<string>('CURRENT_ENVIRONMENT');

{
    provide: CURRENT_ENVIRONMENT,

    useFactory: () => {

        return 'production';

    }
}
How it works

Instead of directly providing a fixed value, Angular calls the factory function to obtain the dependency value.

8. Optional Token Dependency

A dependency can be marked optional when the application should continue working even if no provider is found for that dependency.

Example
import {
    Component,
    inject
} from '@angular/core';

import {
    APP_CONFIG
} from './app-config';

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

    config = inject(
        APP_CONFIG,
        { optional: true }
    );

}
Use Case

Optional dependencies can be useful when a configuration or feature is not guaranteed to have a provider.

9. InjectionToken vs Service

InjectionToken Service
Represents a value, configuration, or abstraction. Usually represents a class containing functionality.
Useful for non-class dependencies. Useful for reusable application logic.
Example: API URL or application configuration. Example: CourseService or TrainingService.
Uses a unique token as the DI key. The class itself can act as the DI token.

10. Common InjectionToken Use Cases

API URL

Store a backend API endpoint as injectable configuration.

API_URL
App Name

Provide application-specific string configuration.

APP_NAME
Configuration

Provide structured application settings.

APP_CONFIG
Feature Flag

Provide feature configuration through dependency injection.

ENABLE_FEATURE

11. How Angular Resolves an InjectionToken

Resolution Flow
Component
    |
    ↓
Requests API_URL
    |
    ↓
Angular Injector
    |
    ↓
Searches Provider
    |
    ↓
API_URL Provider Found
    |
    ↓
Returns Registered Value
    |
    ↓
Component Receives Value
Token API_URL
→
Provider useValue
→
Injector Resolves Token
→
Component Receives Value

Complete CIIT Training Institute Example

Suppose the CIIT Training Institute Angular application needs a configurable API URL and institute name. InjectionToken can be used to provide these values through Angular Dependency Injection.

tokens.ts
import { InjectionToken }
from '@angular/core';

export const API_URL =
    new InjectionToken<string>('API_URL');

export const INSTITUTE_NAME =
    new InjectionToken<string>('INSTITUTE_NAME');
app.config.ts
export const appConfig: ApplicationConfig = {

    providers: [

        {
            provide: API_URL,
            useValue:
                'https://api.example.com'
        },

        {
            provide: INSTITUTE_NAME,
            useValue:
                'CIIT Training Institute'
        }

    ]

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

import {
    API_URL,
    INSTITUTE_NAME
} from './tokens';

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

    apiUrl = inject(API_URL);

    instituteName =
        inject(INSTITUTE_NAME);

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

<p>
    API URL:
    {{ apiUrl }}
</p>
Expected Output:

CIIT Training Institute

API URL: https://api.example.com

InjectionToken Quick Reference

Concept Purpose Example
InjectionToken Creates a unique DI identifier new InjectionToken<string>()
provide Associates a provider with the token provide: API_URL
useValue Provides a fixed value useValue: 'API'
useFactory Creates the dependency using a factory useFactory
inject() Retrieves the token value inject(API_URL)

Important Points About InjectionToken

  • InjectionToken creates a unique identifier for a dependency.
  • It is useful for values and dependencies that do not have a class type at runtime.
  • Tokens can represent strings, objects, configuration, and other values.
  • Providers associate values or factories with the token.
  • Components can retrieve token values through Dependency Injection.
  • InjectionToken is especially useful for configurable application settings.

InjectionToken Best Practices

  • Use clear and descriptive token names.
  • Keep configuration tokens grouped in a meaningful file.
  • Use appropriate generic types for token values.
  • Avoid creating duplicate tokens for the same dependency.
  • Keep provider configuration easy to understand and maintain.
  • Use InjectionToken when a dependency cannot be represented naturally by a class.

Summary

Angular InjectionToken provides a unique identifier that can be used by the Dependency Injection system for values, configuration objects, and other non-class dependencies.

A token is associated with a provider using options such as useValue or useFactory, and the resulting dependency can then be injected into components or services.