Video lecture
Watch on YouTube(opens in a new tab)Sub topic 7
How the App Starts: main.ts and app.config.ts
This chapter assumes you have created a project with ng new and can run it with ng serve. It also assumes you can read ES module imports and TypeScript classes. Here you will follow the path from the browser loading a page to Angular drawing your root component on screen.
The Startup Chain
Three files work together when the app starts:
| File | Role |
|---|---|
| `src/index.html` | The single HTML page. It contains a placeholder element, `<app-root></app-root>`. |
| `src/main.ts` | The entry point. The build compiles it first, and it calls `bootstrapApplication`. |
| `src/app/app.config.ts` | Holds the application-wide providers, such as the router and error handling. |
The root component, App in src/app/app.ts, is the fourth piece. Its selector must match the placeholder tag in index.html.
main.ts: The Entry Point
// src/main.ts
import { bootstrapApplication } from '@angular/platform-browser';
import { appConfig } from './app/app.config';
import { App } from './app/app';
bootstrapApplication(App, appConfig)
.catch((err) => console.error(err));bootstrapApplication takes two arguments:
- The root component class. Angular creates this component first, and every other component is rendered inside it.
- An
ApplicationConfigobject whoseprovidersarray sets up the services the whole app can use.
The call returns a Promise. If startup fails, for example because a provider throws an error, the .catch logs the error. Without it, the page stays blank and gives you no clue why.
app.config.ts: Application-Wide Providers
// src/app/app.config.ts
import { ApplicationConfig, provideBrowserGlobalErrorListeners } from '@angular/core';
import { provideRouter } from '@angular/router';
import { routes } from './app.routes';
export const appConfig: ApplicationConfig = {
providers: [
provideBrowserGlobalErrorListeners(),
provideRouter(routes),
],
};Each provideX() function returns a set of providers that switches on a feature. For example, provideRouter registers the router and provideHttpClient registers the HTTP client. The exact list your CLI generates depends on its version and on the answers you gave to ng new. Angular adds these providers to the root injector, so any component or service in the app can inject them.
Keeping the configuration in its own file keeps main.ts short. It also lets tests or server-side rendering reuse the same configuration.
Common Errors
| Symptom | Cause | Fix |
|---|---|---|
| Console error: `The selector "app-root" did not match any elements` | The root component's `selector` differs from the tag in `index.html` | Use the same name in both files |
| `NullInjectorError: No provider for HttpClient!` | The feature was never added to `appConfig` | Add `provideHttpClient()` to `providers` |
| `<router-outlet>` renders nothing | `provideRouter` is missing or `routes` is empty | Register it in `app.config.ts` |
| Blank page with no visible error | `.catch` was removed from `main.ts` | Keep the error handler and check the console |
Exercise
You rename the root component's selector from app-root to my-root but leave index.html unchanged. What happens, and what are two ways to fix it?
Show answer
Bootstrap fails because Angular cannot find a <my-root> element in the page. The console shows "The selector "my-root" did not match any elements" and nothing renders. You can fix it by changing the tag in index.html to <my-root></my-root>, or by changing the selector back to app-root.
Exercise
A component calls inject(HttpClient) and the app crashes with NullInjectorError. Which file should you edit, and what should you add?
Show answer
Edit src/app/app.config.ts and add provideHttpClient() (imported from @angular/common/http) to the providers array. This registers HttpClient in the root injector.