Sub topic 4
Inside the src folder
This chapter assumes you have run ng new and can open the generated workspace. You should know basic TypeScript, HTML, and how a component pairs a class with a template. The src folder holds everything the build compiles into your application. Files outside it, such as angular.json and package.json, configure the tools.
What the folder contains
A fresh workspace from Angular 17 to 19 looks roughly like this:
src/
├── app/
│ ├── app.component.ts
│ ├── app.component.html
│ ├── app.component.css
│ ├── app.component.spec.ts
│ ├── app.config.ts
│ └── app.routes.ts
├── index.html
├── main.ts
└── styles.cssStatic files such as the favicon live in public/ at the workspace root in Angular 18 and later. In Angular 17 they lived in src/assets/ and src/favicon.ico. Angular 20 and later name the root files app.ts, app.html, app.css and app.spec.ts, and the class is App. The structure is otherwise the same. Check your own workspace before following paths literally.
| Path | Role |
|---|---|
| `index.html` | The single HTML page the browser loads |
| `main.ts` | The entry point that starts the application |
| `styles.css` | Global styles, listed in `angular.json` |
| `app/` | Your application code |
| `app/app.config.ts` | Application-wide providers |
| `app/app.routes.ts` | The route table |
| `*.spec.ts` | Unit tests that sit next to the code they test |
index.html: the host page
Angular builds a single-page application, so the browser loads one HTML file and Angular fills it in. The generated file is short:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>MyApp</title>
<base href="/">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="icon" type="image/x-icon" href="favicon.ico">
</head>
<body>
<app-root></app-root>
</body>
</html>You will not see script tags. The build adds the compiled JavaScript bundles to this file when it writes the output. The two lines that matter are <base href="/"> and <app-root>.
<base href="/">tells the router how to resolve relative URLs. If you deploy under a sub-path such as/shop/, change it, or pass--base-href /shop/tong build.<app-root>is a placeholder. The root component's selector must match this tag exactly, or Angular has nothing to render into.
main.ts: the entry point
angular.json points the build at src/main.ts. The file is only a few lines:
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 the root component and a configuration object. It creates the root injector from the providers in that object, finds the element matching the component's selector, and renders the component there. The .catch reports startup failures, such as a missing provider, to the console.
The next chapter covers bootstrapping in detail. Here, note only that main.ts imports from ./app/..., so the entry point and the application code are separate parts of src.
How the files connect
The diagram shows the order in which the pieces run when the page loads.
The app folder
app/ holds the root component and configuration. In a new workspace the root component is the only component. A minimal version looks like this:
import { Component } from '@angular/core';
import { RouterOutlet } from '@angular/router';
@Component({
selector: 'app-root',
imports: [RouterOutlet],
templateUrl: './app.component.html',
styleUrl: './app.component.css',
})
export class AppComponent {
title = 'my-app';
}The selector ties this class to the <app-root> tag in index.html. The prefix app comes from the prefix setting in angular.json, and ng generate uses it for every component it creates. styleUrl (singular) is the form used in Angular 17 and later, and styleUrls with an array still works.
The component is standalone, so it lists what its template uses in imports. Without RouterOutlet in that array, a <router-outlet> tag in the template would fail to compile.
app.config.ts and app.routes.ts stay small at first. Application-wide providers go in app.config.ts, and URL-to-component mappings go in app.routes.ts. Both are plain TypeScript files, so you can split them as they grow.
Global styles and static files
styles.css applies to the whole page, unlike a component's own stylesheet. The build only includes it because angular.json lists it:
"styles": ["src/styles.css"]A new .css file in src is not picked up until you add it to this array. The same applies to static files. Anything in public/ (or src/assets/ in older versions) is copied to the output as-is. A file at public/logo.png is served at /logo.png. In Angular 17, a file at src/assets/logo.png was served at /assets/logo.png, because the asset glob preserved the folder name.
Spec files and what they imply
app.component.spec.ts is a unit test, placed next to the class it tests. The CLI generates one for each component, service and similar artifact unless you pass --skip-tests. The test builder picks up any file ending in .spec.ts, so you can keep tests anywhere in src. Keeping them beside the code makes them easy to find and move together.
Common errors
- Blank page, no error from the build. The component selector and the tag in
index.htmldo not match. Check the console for a message such as "The selector app-root did not match any elements". - Template element not recognised. A standalone component uses another component or directive that is not in its
importsarray. - New file ignored by the build. TypeScript files outside the paths covered by
tsconfig.app.jsonare not compiled. Keep application code undersrc. - Styles missing. A global stylesheet was added to
srcbut not to thestylesarray inangular.json. - Entry point moved, build fails. If you rename or move
main.ts, update thebrowseroption of the build target inangular.json.
Experiment: change the root component's selector
A quick way to see the connection is to break it on purpose. In app.component.ts, change the selector to app-shell, then run ng serve. The page will be blank, and the console will report that no element matched. Change <app-root> in index.html to <app-shell>, and the application renders again. Revert both changes afterwards, because the prefix and tag name are conventions other tools expect.
Exercise
A teammate renames src/main.ts to src/start.ts, and ng build fails because it cannot find the entry point. Which file do you edit, and which option do you change? Why does the page also stay blank if the root component's selector is changed without updating index.html?
Show answer
Edit angular.json and change the browser option of the build target's options from src/main.ts to src/start.ts. The page stays blank because bootstrapApplication looks for an element in index.html that matches the root component's selector. If no element matches, there is nowhere to render, and Angular logs an error to the console.