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 Providers

Angular Providers tell the Dependency Injection system how a dependency should be created and supplied when a component or service requests it.

What is a Provider?

A provider is a configuration that tells Angular how to obtain a dependency when it is requested through Dependency Injection.

Providers can supply class instances, values, existing services, or values created through factory functions.

Simple Definition

A Provider tells Angular what should be provided when a dependency is requested.

Angular Provider Flow

Component Requests dependency
→
Provider Defines how to provide
→
Injector Resolves dependency
→
Dependency Provided to component

Common Provider Types

Class Provider

Provides an instance of a class.

useClass
Value Provider

Provides a fixed value or configuration object.

useValue
Factory Provider

Creates a dependency using a factory function.

useFactory
Existing Provider

Reuses an already registered dependency.

useExisting

1. Basic Provider Configuration

A provider can associate a dependency token with the class, value, or factory Angular should use.

Example
providers: [
    CourseService
]
How it works

Angular can use the listed service as a dependency provider for the injector associated with that configuration.

2. useClass Provider

The useClass provider tells Angular to use a particular class when a dependency token is requested.

Service
export class CourseService {

    getCourse(): string {

        return 'Java Full Stack';

    }

}
Provider
providers: [

    {
        provide: CourseService,
        useClass: CourseService
    }

]
Simple Idea

useClass allows Angular to associate a token with the class that should be instantiated.

3. Providing an Alternative Class

One token can be configured to use a different implementation class.

Services
export class CourseService {

    getCourse(): string {

        return 'Java Full Stack';

    }

}

export class AdvancedCourseService {

    getCourse(): string {

        return 'Advanced Angular Full Stack';

    }

}
Provider
providers: [

    {
        provide: CourseService,
        useClass: AdvancedCourseService
    }

]
Provided Implementation:

Course: Advanced Angular Full Stack

4. useValue Provider

The useValue provider supplies a fixed value directly through Dependency Injection.

Provider
providers: [

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

]
Use Case

useValue is useful for strings, numbers, configuration objects, feature flags, and other fixed values.

5. useValue with Configuration

A configuration object can be provided using useValue.

Configuration
const appConfig = {

    instituteName:
        'CIIT Training Institute',

    environment:
        'production',

    enableLogging:
        true

};
Provider
providers: [

    {
        provide: APP_CONFIG,
        useValue: appConfig
    }

]
Configuration:

Institute: CIIT Training Institute

Environment: production

Logging: Enabled

6. useFactory Provider

A useFactory provider uses a function to create or calculate the dependency value.

Factory Function
function createApiUrl(): string {

    return 'https://api.example.com';

}
Provider
providers: [

    {
        provide: API_URL,
        useFactory: createApiUrl
    }

]
How it works

Angular calls the factory function to obtain the dependency value when the provider needs to create it.

7. Factory Provider with Dependencies

A factory provider can itself depend on other registered services or values.

Factory
function createMessage(
    trainingService: TrainingService
): string {

    return trainingService
        .getInstituteName();

}
Provider
providers: [

    {
        provide: APP_MESSAGE,
        useFactory: createMessage,
        deps: [TrainingService]
    }

]
Important

The deps array tells Angular which dependencies should be provided to the factory function.

8. useExisting Provider

The useExisting provider creates an alias for an already registered dependency instead of creating another instance.

Provider
providers: [

    CourseService,

    {
        provide: TrainingService,
        useExisting: CourseService
    }

]
Simple Idea

useExisting tells Angular to use an existing instance associated with another provider.

9. Provider Types at a Glance

useClass Provide a class
useValue Provide a value
useFactory Create using function
useExisting Reuse existing instance

10. Provider with InjectionToken

InjectionToken can be used together with providers to inject values or configuration objects that do not have a suitable class token.

Token
import { InjectionToken }
from '@angular/core';

export const API_URL =
    new InjectionToken<string>('API_URL');
Provider
providers: [

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

]
Connection

The InjectionToken identifies the dependency, while the Provider tells Angular what value should be associated with that token.

11. Component-Level Providers

Providers can be declared in a component when the dependency should be resolved within that component's injector hierarchy.

dashboard.component.ts
@Component({

    selector: 'app-dashboard',

    providers: [
        CourseStateService
    ],

    templateUrl:
        './dashboard.component.html'

})
export class DashboardComponent {

    constructor(
        private courseStateService:
            CourseStateService
    ) {

    }

}
Scope:
The provider is associated with the DashboardComponent injector hierarchy.

12. Application-Level Providers

Providers can also be configured at application startup so that they are available through the application's injector hierarchy.

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

export const appConfig:
    ApplicationConfig = {

    providers: [

        CourseService,

        TrainingService

    ]

};
Use Case

Application-level providers are useful for dependencies that need to be registered during application configuration.

13. provide and Token

The provide property identifies the dependency token that Angular should register.

Example
providers: [

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

]
provide API_URL
→
Provider Defines strategy
→
Injector Registers dependency

14. Provider Comparison

Provider Purpose Example
useClass Provides a class implementation useClass: MyService
useValue Provides a fixed value useValue: 'CIIT'
useFactory Creates dependency using a function useFactory: createValue
useExisting Reuses an existing dependency useExisting: ExistingService

Complete CIIT Training Institute Example

Suppose the CIIT Training Institute Angular application needs a course service and an application configuration value. Providers can define how both dependencies should be supplied.

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

@Injectable()
export class TrainingService {

    getInstituteName(): string {

        return 'CIIT Training Institute';

    }

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

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

    providers: [

        TrainingService,

        {
            provide: TRAINING_MODE,
            useValue:
                'Online + Offline'
        }

    ]

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

import { TrainingService }
from './training.service';

import { TRAINING_MODE }
from './tokens';

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

    private trainingService =
        inject(TrainingService);

    trainingMode =
        inject(TRAINING_MODE);

    instituteName =
        this.trainingService
            .getInstituteName();

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

<p>
    Training Mode:
    {{ trainingMode }}
</p>
Expected Output:

CIIT Training Institute

Training Mode: Online + Offline

15. Choosing the Right Provider

UseClass

Use when Angular should provide a class implementation.

useClass
UseValue

Use for fixed values and application configuration.

useValue
UseFactory

Use when the dependency needs to be created dynamically.

useFactory
UseExisting

Use when an existing service instance should be reused.

useExisting

Important Points About Providers

  • Providers define how Angular should supply dependencies.
  • useClass provides a class implementation.
  • useValue provides a fixed value or object.
  • useFactory creates a dependency using a function.
  • useExisting reuses an already registered dependency.
  • Providers can be configured at different levels of the Angular injector hierarchy.

Provider Best Practices

  • Choose the provider type according to the dependency requirement.
  • Use meaningful InjectionToken names for value-based dependencies.
  • Keep provider configuration easy to read and maintain.
  • Use application-wide providers only when broad availability is actually required.
  • Use component-level providers when localized service state is appropriate.
  • Avoid unnecessary duplicate service instances.

Summary

Angular Providers define how dependencies should be created, supplied, or reused by the Dependency Injection system.

The important provider strategies are useClass, useValue, useFactory, and useExisting.