Blog

Part 1 · Getting Started with Angular 22: Your First Component and How the App Starts

Make the practice project every later part uses, see how index.html, main.ts and the App component fit together, write your first component, and read your first build error.

Angular is a framework for building web pages that change as people use them. You write components: small pieces of page, each one a TypeScript class with an HTML template. Angular puts them on the page, keeps the page in step with your data, and gives you a router, forms, HTTP and testing in one box. It’s made by a team at Google and released twice a year. This series uses Angular 22.3.

This part sets up the one project you’ll use for the whole series. We make it with ng new, run it with ng serve, walk through the files that matter, and follow how a page that starts empty ends up showing your component. Then we write a first component of our own and read the two build errors you’re most likely to meet first. Every example here is a whole file you can paste into the practice project you make below. Each one was built with Angular 22.3 and run in a browser before this was published.

Try this first

Here’s the <body> of the index.html file in a new Angular project. It’s the page the browser loads:

<body>
  <app-root></app-root>
</body>

And here’s the start of the component the project comes with, in src/app/app.ts:

@Component({
  selector: 'app-root',
  template: `<h1>Hello, {{ title() }}</h1>`,
})

When you open the app, the page says “Hello, practice”. Before you read on, guess: if you ask the browser for the page’s source (right-click, View page source), will “Hello, practice” be in it? We’ll check once the project is running.

What you need

Node.js. Angular’s tools run on Node.js, and Angular 22.3 supports these versions:

  • Node 22, version 22.22.3 or later;
  • Node 24, version 24.15.0 or later;
  • Node 26 or later.

That’s the engines field in the package.json of @angular/core and @angular/cli 22.3. Odd-numbered versions like 23 and 25 aren’t on the list. Check yours in a terminal:

node --version

If it’s older, install the current LTS (“long-term support”) version from nodejs.org. Node comes with npm, the tool that downloads JavaScript packages, and npx, which runs a command from a package without installing it for good.

An editor. Any editor works. VS Code with Angular’s own extension, Angular Language Service, is a good choice: it checks your templates as you type and lets you jump from a template to the class. The new project even recommends it when you open the folder in VS Code.

A terminal. You’ll type a few commands. On Windows, PowerShell is fine.

Make the practice project

Open a terminal in the folder where you keep your code and run:

npx @angular/cli@22 new practice --inline-template --inline-style

Here’s what that says:

  • npx @angular/cli@22 runs version 22 of the Angular CLI (command-line interface) without installing it for good. npx may ask to download it first; answer y.
  • new practice makes a new project in a folder called practice.
  • --inline-template puts each component’s HTML template inside its .ts file, instead of a separate .html file.
  • --inline-style does the same for its CSS.

Those two flags matter for this series. Every example in it is a whole src/app/app.ts with the template inside, so you replace one file and you’re done. They also tell ng generate component to make new components the same way.

ng new then asks some questions. Press Enter for the default each time:

Question Answer
Would you like to share pseudonymous usage data … ? (only the first time you use the CLI) your choice; the default is No
Which stylesheet system would you like to use? CSS
Do you want to enable Server-Side Rendering (SSR) and Static Site Generation (SSG/Prerendering)? No
Which AI tools should Angular integrate with? None

Server-side rendering is Part 29. For everything before it, you want it off. If you’d rather not answer questions, this does the same thing in one go:

npx @angular/cli@22 new practice --inline-template --inline-style --style=css --ssr=false --ai-config=none

The CLI writes the files, installs the packages and makes a Git repository. The end of the output looks like this (trimmed):

CREATE practice/README.md (1461 bytes)
CREATE practice/angular.json (2033 bytes)
CREATE practice/package.json (784 bytes)
...
CREATE practice/src/main.ts (222 bytes)
CREATE practice/src/index.html (294 bytes)
CREATE practice/src/styles.css (80 bytes)
CREATE practice/src/app/app.spec.ts (682 bytes)
CREATE practice/src/app/app.ts (327 bytes)
CREATE practice/src/app/app.config.ts (312 bytes)
CREATE practice/src/app/app.routes.ts (77 bytes)
CREATE practice/public/favicon.ico (15086 bytes)
✔ Packages installed successfully.
    Successfully initialized git.

Installing takes a minute or two. On 4 December 2026, this installed Angular 22.3.2.

Run it with ng serve

Go into the folder and start the development server:

cd practice
npx ng serve

npx ng serve runs the copy of the CLI that ng new installed inside the project. npm start does the same thing. If you install the CLI for your whole machine with npm install -g @angular/cli@22, you can type plain ng serve. The rest of this series writes ng serve; type whichever form works for you.

It builds the app and then waits:

✔ Building...
Initial chunk files | Names         | Raw size
main.js             | main          |  2.55 kB |
styles.css          | styles        | 95 bytes |

                    | Initial total |  2.64 kB

Application bundle generation complete. [1.229 seconds] - 2026-12-04T08:53:51.875Z

Watch mode enabled. Watching for file changes...
NOTE: Raw file sizes do not reflect development server per-request transformations.
  ➜  Local:   http://localhost:4200/

Open http://localhost:4200/ in your browser. The page says Hello, practice in big letters. Leave ng serve running while you work. It watches your files and rebuilds when you save. To stop it, press Ctrl+C in the terminal.

Now the guess from the start. Right-click the page and choose View page source. The body is:

<body>
  <app-root></app-root>
<script src="main.js" type="module"></script></body>

“Hello, practice” isn’t there. The page the server sends has an empty <app-root> tag and a script. The text is put there by that script, in your browser, after the page loads. The rest of this part follows how.

(Your browser’s developer tools, F12, show the page as it is now, with the <h1> inside <app-root>. View page source shows what the server sent.)

The files that matter

Here’s the project, trimmed to what you’ll touch:

practice/
  angular.json
  package.json
  tsconfig.json          (and tsconfig.app.json, tsconfig.spec.json)
  node_modules/          the installed packages; never edit these
  public/
    favicon.ico
  src/
    index.html
    main.ts
    styles.css
    app/
      app.config.ts
      app.routes.ts
      app.spec.ts
      app.ts

We’ll take them in the order the browser meets them.

src/index.html is the one HTML page of the app. Its body holds a single <app-root></app-root> tag. That’s a custom tag: the browser doesn’t know it and shows nothing for it. It’s a placeholder that Angular will fill.

src/main.ts is where the app’s code starts:

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(App, appConfig) starts Angular with App as the root component, the one at the top of the page, and appConfig as its settings. If starting fails, the .catch logs the error to the browser’s console. You won’t edit this file in this series.

src/app/app.config.ts holds those settings:

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 entry in providers switches on a feature for the whole app. provideBrowserGlobalErrorListeners() passes errors that nothing else caught to Angular’s error handler, which logs them to the console. provideRouter(routes) switches on the router. Later parts add more here, such as provideHttpClient() in Part 18. Part 13 explains what “providing” means.

src/app/app.routes.ts is the list of routes, which page to show for which URL. In a new project it’s an empty list, export const routes: Routes = [];. Part 16 fills it.

src/app/app.ts is the root component, and the file you’ll edit most:

// src/app/app.ts
import { Component, signal } from '@angular/core';
import { RouterOutlet } from '@angular/router';

@Component({
  imports: [RouterOutlet],
  selector: 'app-root',
  styles: [],
  template: `
    <h1>Hello, {{ title() }}</h1>

    <router-outlet />
  `,
})
export class App {
  protected readonly title = signal('practice');
}

The page shows:

Hello, practice

The first line, // src/app/app.ts, isn’t in the file ng new made. It’s a comment, and this series starts every file with one so you know where the file goes. Pasting it in does no harm.

A component is a class with a @Component({...}) above it. That @ line is a decorator: it attaches Angular’s settings to the class. The settings that matter now:

  • selector: 'app-root' is the tag this component fills. It matches the <app-root> in index.html. That’s the whole connection between the two files.
  • template is the component’s HTML. The backticks make it a multi-line string. Double curly braces put a value from the class into the text; Part 2 covers them.
  • imports lists the other components (and directives and pipes) this template uses. <router-outlet /> is where the router shows the current route’s page, and RouterOutlet is the class behind that tag. With no routes, it shows nothing.
  • styles is the component’s CSS. It’s empty here. Part 7 covers styling.

Inside the class, title holds signal('practice'). A signal is a value Angular can watch for changes; that’s why the template calls it, title(). Part 3 covers signals. For now, read title() as “the current title”. protected keeps title out of the class’s public API, but its template can still use it. readonly means title can’t be replaced by something else later.

src/app/app.spec.ts is a test for App. It checks that the page has an <h1> containing “Hello, practice”. We come back to it below, because it stops being true as soon as you change app.ts.

src/styles.css is CSS for the whole app. It’s empty apart from a comment.

angular.json tells the CLI how to build, serve and test the project; you won’t need to edit it. package.json lists the packages the project uses, with their versions, and the npm start, npm test and npm run build commands.

How the app starts

Put together, this is what happens when you open http://localhost:4200/:

1. src/index.html <body><app-root></app-root></body> the browser loads an empty tag and the main.js script 2. src/main.ts bootstrapApplication(App, appConfig) app.config.ts providers: […] 3. src/app/app.ts: the App component selector: 'app-root' template: <h1>Hello, {{ title() }}</h1> the selector names the tag; the template fills it 4. the page in the browser <app-root><h1>Hello, practice</h1></app-root> Angular puts the template inside the tag

The server sends an empty tag. main.ts starts Angular, and Angular fills the tag that matches App’s selector.

In words:

  1. The browser loads index.html. Its body has an empty <app-root> tag and a <script> for main.js, which is your TypeScript, built into JavaScript by the CLI.
  2. The script runs main.ts, which calls bootstrapApplication(App, appConfig). Angular switches on everything in appConfig.providers.
  3. Angular looks at App‘s selector, 'app-root', and finds that tag in the page.
  4. Angular renders App‘s template inside it. The <h1> you see lives inside <app-root>.

Every later part works inside step 4. You change app.ts, and what’s inside <app-root> changes.

Your first edits

Before you paste anything, deal with app.spec.ts. It tests that the page says “Hello, practice”. The moment you change the heading, that test fails. You won’t see the failure while you use ng serve, because ng serve doesn’t run tests, but ng test will report it:

 FAIL  |practice| src/app/app.spec.ts > App > should render title
AssertionError: expected 'Hello, Angular' to contain 'Hello, practice'

The tests in this series start in Part 24, and they come with their own .spec.ts files. So delete src/app/app.spec.ts now. Until Part 24, ng test then stops with No tests found matching the following patterns. That’s expected.

Now replace all of src/app/app.ts with this, and save:

// src/app/app.ts
import { Component } from '@angular/core';

@Component({
  selector: 'app-root',
  template: `
    <h1>Hello, Angular</h1>
    <p>This text comes from the App component.</p>
  `,
})
export class App {}

The browser updates by itself. The page shows:

Hello, Angular
This text comes from the App component.

A few things went away, on purpose. There’s no imports, because the template uses no other components. There’s no styles, because there’s no CSS. And there’s no <router-outlet />, because we have no routes yet. The router is still switched on in app.config.ts, which does no harm. Every later part’s app.ts looks like this: just what the example needs.

Look at the terminal where ng serve is running. When you change only a template, it updates the component in place, without reloading the page:

✔ Changes detected. Rebuilding...
Application bundle generation complete. [0.203 seconds] - 2026-12-04T08:56:07.139Z

Component update sent to client(s).

When you change the class, it reloads the whole page instead, and the last line is Page reload sent to client(s). Either way, you save and look.

Showing a value from the class

The class can hold data for the template. Replace app.ts again:

// src/app/app.ts
import { Component } from '@angular/core';

@Component({
  selector: 'app-root',
  template: `
    <h1>Hello, {{ name }}</h1>
    <p>You are learning Angular {{ version }}.</p>
  `,
})
export class App {
  protected readonly name = 'Ana';
  protected readonly version = 22;
}

The page shows:

Hello, Ana
You are learning Angular 22.

name and version are ordinary class fields. Inside double curly braces, the template can use any field of its class by name, without this.. They never change here, so plain fields are fine. Values that change go in signals, from Part 3 on.

Your first component in its own file

A real page is built from many components, each in its own file. Make a new file, src/app/greeting.ts:

// src/app/greeting.ts
import { Component } from '@angular/core';

@Component({
  selector: 'app-greeting',
  template: `<p>Hello from the Greeting component.</p>`,
})
export class Greeting {}

The names follow Angular’s style guide: the file is greeting.ts, the class is Greeting, and the selector is app-greeting. The app- prefix keeps your tags apart from real HTML tags and from other libraries’ tags, and the CLI uses it for every component it makes.

To use it, App needs two things: a TypeScript import of the class, and the class in its imports list. Then its template can use the tag. Replace app.ts:

// src/app/app.ts
import { Component } from '@angular/core';
import { Greeting } from './greeting';

@Component({
  selector: 'app-root',
  imports: [Greeting],
  template: `
    <h1>Hello, Angular</h1>
    <app-greeting />
    <app-greeting />
  `,
})
export class App {}

The page shows:

Hello, Angular
Hello from the Greeting component.
Hello from the Greeting component.

<app-greeting /> is written as a self-closing tag. Angular allows that for components; <app-greeting></app-greeting> means the same. Each tag makes its own Greeting, so two tags give two paragraphs.

Now App is the parent and each Greeting is a child. The page is a tree of components: App at the top, its children under it. Later parts pass data down that tree (Part 5) and send events back up (Part 6).

If you open the developer tools (F12) and look at the Elements tab, you’ll see each paragraph sits inside its own <app-greeting> element, inside <app-root>:

<app-root>
  <h1>Hello, Angular</h1>
  <app-greeting><p>Hello from the Greeting component.</p></app-greeting>
  <app-greeting><p>Hello from the Greeting component.</p></app-greeting>
</app-root>

A component’s tag stays in the page and holds its template.

The CLI can make components too. ng generate component greeting writes src/app/greeting/greeting.ts, in a folder of its own, with the same class and selector names and a placeholder template, plus a greeting.spec.ts test. In this series, paste the files instead, so your paths and code match the post.

Worked example: a component nobody imported

The two lines that connect a component to its parent are easy to forget. Here’s App using <app-greeting /> without them:

// src/app/greeting.ts
import { Component } from '@angular/core';

@Component({
  selector: 'app-greeting',
  template: `<p>Hello from the Greeting component.</p>`,
})
export class Greeting {}
// src/app/app.ts
import { Component } from '@angular/core';

@Component({
  selector: 'app-root',
  template: `
    <h1>Hello, Angular</h1>
    <app-greeting />
  `,
})
export class App {}

The build stops, and ng serve prints:

✘ [ERROR] NG8001: 'app-greeting' is not a known element:
1. If 'app-greeting' is an Angular component, then verify that it is included in the '@Component.imports' of this component.
2. If 'app-greeting' is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' to the '@Component.schemas' of this component to suppress this message. Find more at https://next.angular.dev/errors/NG8001 [plugin angular-compiler]

The browser shows the same message in a box over the page. Under the box, the page keeps the last version that built. Fix the code and the box goes away by itself.

Read the error from the top:

  • ✘ [ERROR] means the build failed. Nothing new reaches the browser until you fix it.
  • NG8001 is Angular’s code for this error. Codes starting with NG come from Angular’s compiler. Codes starting with TS come from TypeScript. The link at the end goes to a page about this exact code. It may point at next.angular.dev, the docs site for the version still in development. That’s fine: the page explains the same error.
  • 'app-greeting' is not a known element says what Angular saw: a tag it can’t match to any component that App knows about.
  • Option 1 is our case: app-greeting is an Angular component, so check it’s in @Component.imports. It isn’t.
  • Option 2 is for Web Components, custom tags made by other, non-Angular code. That’s not us, so we skip it.

Below those lines, the output points at the spot: the file, the line, and a ~~~ under the tag. The file is app.ts, not greeting.ts. That’s the key point. Greeting is fine. The component that uses the tag is the one missing the import.

The fix is the version from the last section: import { Greeting } from './greeting'; at the top, and imports: [Greeting] in @Component. Each component lists what its own template uses. A component imported by App isn’t automatically available inside Greeting‘s template.

Worked example: a typo in the template

Now a component with a field. Greeting gets a name, but the template spells it wrong:

// src/app/greeting.ts
import { Component } from '@angular/core';

@Component({
  selector: 'app-greeting',
  template: `<p>Hello, {{ nmae }}!</p>`,
})
export class Greeting {
  protected readonly name = 'Ana';
}
// src/app/app.ts
import { Component } from '@angular/core';
import { Greeting } from './greeting';

@Component({
  selector: 'app-root',
  imports: [Greeting],
  template: `<app-greeting />`,
})
export class App {}

ng serve refuses to build it:

✘ [ERROR] TS2339: Property 'nmae' does not exist on type 'Greeting'. [plugin angular-compiler]

    src/app/greeting.ts:6:26:
      6 │   template: `<p>Hello, {{ nmae }}!</p>`,
        ╵                           ~~~~

This one starts with TS, so it’s a TypeScript error. Angular turns each template into TypeScript and type-checks it, so a typo in a template is caught at build time, the same as a typo in a class. The message names the missing property, nmae, and the class it looked in, Greeting. src/app/greeting.ts:6:26 is the file, line 6, column 26, and the ~~~~ underlines nmae. (The real output starts the path with your project’s folder.)

Without this check, a misspelled name would just show nothing on the page, and you might not notice for a while. Here the build won’t finish until you fix it. Change nmae to name:

// src/app/greeting.ts
import { Component } from '@angular/core';

@Component({
  selector: 'app-greeting',
  template: `<p>Hello, {{ name }}!</p>`,
})
export class Greeting {
  protected readonly name = 'Ana';
}
// src/app/app.ts
import { Component } from '@angular/core';
import { Greeting } from './greeting';

@Component({
  selector: 'app-root',
  imports: [Greeting],
  template: `<app-greeting />`,
})
export class App {}

The page shows:

Hello, Ana!

A habit worth starting now: when the build fails, read the first ✘ [ERROR] line, then the file and line under it. Fix that one and save. One mistake can cause several errors, and the later ones often go away with the first.

In older code

Projects made before Angular 20 name files app.component.ts, app.component.html and classes AppComponent. Projects before Angular 17 often have an app.module.ts with @NgModule({ declarations: [...] }): components were listed in a module instead of in each other’s imports. A component that is part of a module has standalone: false in its @Component. New projects don’t use modules, and every component in this series is standalone, the default since Angular 19.

Best practices

  • Make one practice project with --inline-template --inline-style, and paste whole files into it. Most examples then replace one file, and nothing is left over from the last one. It replaces juggling a separate app.html and app.css for every example.
  • Keep ng serve running in its own terminal, and look at it after each save. Errors and warnings appear there first. It replaces reloading the browser and guessing why nothing changed.
  • Put one component in one file, named after it: greeting.ts, class Greeting, selector app-greeting. You can find any tag’s code from its name. It replaces the older greeting.component.ts naming you’ll see in existing projects.
  • List in imports exactly what the template uses. Add a component when you add its tag, and remove it when you remove the tag. The build tells you about both mistakes.
  • Read build errors from the top, code first. NG codes are Angular’s, TS codes are TypeScript’s, and the file and line say where.

Tips and traps

  • Pasting app.ts but keeping the old app.spec.ts. ng serve is fine, but ng test fails with expected 'Hello, Angular' to contain 'Hello, practice'. Delete app.spec.ts; Part 24 brings its own tests.
  • Made the project without the two flags? You’ll have app.html and app.css next to app.ts. Once you paste an app.ts with an inline template, nothing reads them, and they cause no warning. Delete them so you don’t edit the wrong file.
  • Forgetting imports: [...]. The build fails with NG8001, 'app-greeting' is not a known element, pointing at the parent’s file. Add the class to the parent’s imports.
  • Importing a component the template doesn’t use. The build still works, but ng serve prints a warning. Remove it from imports. (Exercise 2 shows the warning.)
  • A typo in a template name. It’s a TS2339 build error, not a blank space on the page. The message names the property and the class it looked in.
  • The page doesn’t change after you save. Look at the terminal: the build probably failed, and the browser is showing the last version that worked. If ng serve isn’t running at all, http://localhost:4200/ won’t load.
  • Port 4200 is already in use. ng serve asks Would you like to use a different port?, and Yes picks a free one and prints it on the Local: line. Usually it means another ng serve is still running in a terminal you forgot. Stop that one with Ctrl+C, so your bookmark for http://localhost:4200/ keeps working.

The server sends an empty <app-root>; Angular fills it with the component whose selector matches.

Exercises

Exercise 1. Make a Footer component in src/app/footer.ts, with the selector app-footer, that shows Made with Angular 22. Use it in App under a heading My page, so the page shows those two lines.

Exercise 2. Go back to the App and Greeting from “Your first component in its own file”, and delete the two <app-greeting /> lines from App‘s template. Leave the import line and imports: [Greeting] as they are. Does it build? What does ng serve print?

Exercise 3. Make a Wave component in src/app/wave.ts (selector app-wave) that shows *waves*. Use it inside Greeting‘s template, after its paragraph, and use <app-greeting /> once in App. Which component’s imports does Wave go in?

Answers

1. A new file for Footer, and App imports it:

// src/app/footer.ts
import { Component } from '@angular/core';

@Component({
  selector: 'app-footer',
  template: `<p>Made with Angular 22</p>`,
})
export class Footer {}
// src/app/app.ts
import { Component } from '@angular/core';
import { Footer } from './footer';

@Component({
  selector: 'app-root',
  imports: [Footer],
  template: `
    <h1>My page</h1>
    <app-footer />
  `,
})
export class App {}

The page shows:

My page
Made with Angular 22

2. It builds, but ng serve prints a warning. Here’s that version:

// src/app/greeting.ts
import { Component } from '@angular/core';

@Component({
  selector: 'app-greeting',
  template: `<p>Hello from the Greeting component.</p>`,
})
export class Greeting {}
// src/app/app.ts
import { Component } from '@angular/core';
import { Greeting } from './greeting';

@Component({
  selector: 'app-root',
  imports: [Greeting],
  template: `<h1>Hello, Angular</h1>`,
})
export class App {}
▲ [WARNING] NG8113: All imports are unused [plugin angular-compiler]

The page still shows:

Hello, Angular

NG8113 is one of Angular’s extended diagnostics: checks for code that’s allowed but almost certainly a mistake. It says “all imports” because Greeting is the only one. When only some are unused, it names them, for example Wave is not used within the template of App. The fix is to remove Greeting from imports and delete its import line, or to put <app-greeting /> back if you meant to use it. An unused import doesn’t break the page, but it tells the next reader that App uses Greeting when it doesn’t.

3. Wave goes in Greeting‘s imports, because Greeting‘s template is the one that uses <app-wave />. App doesn’t import Wave at all:

// src/app/wave.ts
import { Component } from '@angular/core';

@Component({
  selector: 'app-wave',
  template: `<p>*waves*</p>`,
})
export class Wave {}
// src/app/greeting.ts
import { Component } from '@angular/core';
import { Wave } from './wave';

@Component({
  selector: 'app-greeting',
  imports: [Wave],
  template: `
    <p>Hello from the Greeting component.</p>
    <app-wave />
  `,
})
export class Greeting {}
// src/app/app.ts
import { Component } from '@angular/core';
import { Greeting } from './greeting';

@Component({
  selector: 'app-root',
  imports: [Greeting],
  template: `<app-greeting />`,
})
export class App {}

The page shows:

Hello from the Greeting component.
*waves*

If you put Wave in App‘s imports instead, you get NG8001 for app-wave, pointing at greeting.ts, and a warning in app.ts that Wave isn’t used there.

What’s next

Our templates so far only show fixed text and a field or two. In Part 2, we’ll fill them with bindings: double curly braces, [property], [class.x], [style.x] and (event) for clicks and typing.

How useful was this post?

Click on a heart to rate it!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate this post.