Sub topic 5
How the application starts: main.ts and the application config
This chapter assumes you can read TypeScript, know what an Angular component is, and have a workspace created with ng new. It traces what happens between ng serve and the first component appearing in the browser.
The startup chain
The browser loads index.html. The CLI build injects the compiled scripts into that file. The first script to run is the compiled src/main.ts, the entry point named in angular.json under the build option browser.
main.ts
import { bootstrapApplication } from '@angular/platform-browser';
import { appConfig } from './app/app.config';
import { AppComponent } from './app/app.component';
bootstrapApplication(AppComponent, appConfig).catch((err) => console.error(err));bootstrapApplication takes two arguments:
- The root component. Angular finds the element that matches its selector, usually
<app-root>inindex.html, and renders the component there. - An optional configuration object that lists application-wide providers.
The call returns a promise. The .catch logs any error thrown during startup, such as a missing provider.
Newer CLI versions name the root file and class app.ts and App instead of app.component.ts and AppComponent. The structure is the same.
The application config
import { ApplicationConfig } from '@angular/core';
import { provideRouter } from '@angular/router';
import { routes } from './app.routes';
export const appConfig: ApplicationConfig = {
providers: [provideRouter(routes)],
};ApplicationConfig is an interface with a providers array. Each entry registers a service or feature with the root injector, which is the dependency injection container for the whole application. Here, provideRouter(routes) registers the router with the routes array from app.routes.ts.
Other features follow the same provideX() pattern. For example, adding provideHttpClient() to the array makes HttpClient injectable anywhere in the app.
import { ApplicationConfig } from '@angular/core';
import { provideRouter } from '@angular/router';
import { provideHttpClient } from '@angular/common/http';
import { routes } from './app.routes';
export const appConfig: ApplicationConfig = {
providers: [provideRouter(routes), provideHttpClient()],
};Why the config lives in its own file
Keeping providers in app.config.ts leaves main.ts short and stable. Tests can import appConfig and reuse or extend it. You rarely edit main.ts after project creation. You edit app.config.ts whenever you add an application-wide capability.
Common errors
NullInjectorError: No provider for HttpClient. The code injects a service that no provider supplies. Add the matchingprovideX()call toappConfig.providers.- Nothing renders and the console shows no error. The root component's selector does not match any element in
index.html. Check that both useapp-root. - A new feature has no effect. You added the provider to a component's
providersarray instead ofappConfig. Component-level providers only apply to that component and its children.
Exercise
You add a service that injects HttpClient, and the app fails at runtime with NullInjectorError: No provider for HttpClient. Which file do you change, and what do you add?
Show answer
Change src/app/app.config.ts and add provideHttpClient() to the providers array, importing it from @angular/common/http.
Exercise
What are the two arguments to bootstrapApplication, and which one decides where on the page the app appears?
Show answer
The root component and the application config. The root component's selector, matched against an element in index.html, decides where the app appears.