Skip to content
Tutorials Center
My learning

Topics

Lesson 4 of 74 ยท Getting Started

Your First Component

Write your first Angular component. Learn the @Component decorator, selector, template, styles and the imports array, and how a decorator works as metadata.

Everything you see in an Angular application is a component. The page itself is a component, a navigation bar is a component, and so is a single button with special behavior. A component combines three things: a TypeScript class that holds data and logic, an HTML template that describes what to show, and CSS styles for that template. You build an app by nesting components inside each other, like HTML elements you design yourself.

In this lesson you write one component, learn what each setting in its @Component decorator means, and use it inside another component. As in the other lessons, the Angular code in plain code blocks is real and read-only (try it in a project from the setup lesson); the runnable examples are stand-ins.

The smallest component

Here is a complete component in a file called greeting.ts:

TypeScript
import { Component } from '@angular/core';

@Component({
  selector: 'app-greeting',
  template: `
    <h2>Hello, {{ name }}!</h2>
    <p>Welcome to Angular.</p>
  `,
  styles: `
    h2 { color: rebeccapurple; }
  `,
})
export class Greeting {
  name = 'reader';
}

Read it from the bottom up. Greeting is an ordinary TypeScript class with one property, name. Above it, @Component({...}) is a decorator that tells Angular "this class is a component" and gives it its settings. Angular needs the class and the settings together; neither works alone.

The {{ name }} in the template is interpolation: Angular reads the name property of the class and puts its value in the page. The next chapter is all about templates.

The @Component settings

Setting Meaning
selector The HTML tag name that places this component, here <app-greeting>
template The HTML, written inline (a backtick string)
templateUrl The path to an HTML file, used instead of template
styles CSS written inline; applies only to this component
styleUrl The path to a CSS file (styleUrls takes several)
imports Other components, directives and pipes this template uses

For anything longer than a few lines, use files, which is what the CLI generates:

TypeScript
@Component({
  selector: 'app-user-card',
  imports: [],
  templateUrl: './user-card.html',
  styleUrl: './user-card.css',
})
export class UserCard {}

Selectors and prefixes

Choose a selector that cannot clash with a real HTML element. Custom element names must contain a hyphen, and Angular projects conventionally start with a prefix (app by default, or your company name for a shared library). So app-greeting, never greeting. Selectors are written in kebab-case, class names in PascalCase.

Standalone by default

Since Angular 19, every component is standalone by default: it declares its own dependencies in imports and does not need to belong to an NgModule. You will still see standalone: true in code from Angular 14 to 18, and standalone: false plus declarations arrays in older module-based apps. In new code, leave the property out.

Using a component inside another

A component only appears where you write its selector. To show Greeting inside the root component, the root must import the class and use the tag:

TypeScript
import { Component } from '@angular/core';
import { Greeting } from './greeting';

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

Two details matter. First, the class goes in the imports array. If you write <app-greeting /> but forget to import Greeting, the compiler stops with an error saying the element is not a known element. This is deliberate: every template lists exactly what it uses. Second, you may write a component as a self-closing tag (<app-greeting />) as well as with a closing tag.

The root component App is the one passed to bootstrapApplication, and it shows up in index.html as <app-root>. The components inside it form a tree.

flowchart TB
  R["app-root<br/>(App)"] --> G["app-greeting<br/>(Greeting)"]
  R --> C["app-user-card<br/>(UserCard)"]
  C --> B["app-badge<br/>(Badge)"]

Components nest like HTML elements. Each box is a host element in the page and a class instance behind it.

Each time Angular meets <app-greeting /> it creates a new instance of the class, so you can use the same component several times and each copy has its own data.

The decorator is metadata

It helps to remember what a decorator is not: it is not magic and it does not run your template. A decorator is a function that is applied to your class when the class is defined. In Angular's case it records the settings (selector, template, imports) so that the compiler and the runtime can find them later. The class stays a plain TypeScript class that you could even create with new Greeting() in a test.

If you want to know how decorators work in TypeScript itself, see the decorators lesson. One historical detail: Angular's compiler reads decorators statically at build time instead of running a decorator library at runtime, so you cannot compute the metadata dynamically. That is why the selector must be a plain string and the template a plain string or file path, not a function call.

Stand-in 1: a component is a class plus metadata

The first stand-in separates the two halves explicitly. The metadata is an object, the class holds the data, and mount combines them into HTML, which is the same contract as a real component, without the compiler.

TypeScript A stand-in: class plus metadata gives HTML

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or readFileSync(0)), it gets the next line from this box.

The two instances are independent: after two increments first shows 5 while second still shows 3. Real Angular goes one step further and re-renders by itself when the data changes. The stand-in needs you to call mount again.

Stand-in 2: decorators and the imports array

This second stand-in uses a real TypeScript decorator to register metadata, like Angular does, and checks the imports list when it meets a child tag. Try removing Badge from Profile's imports to see the error that Angular's compiler also gives you.

TypeScript A stand-in: @Component registers metadata, imports must list children

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or readFileSync(0)), it gets the next line from this box.

Profile renders because Badge is in its imports; Broken uses the same tag without importing it and fails. The real Angular compiler reports this at build time, before the user ever sees a page, which is a good reason to keep templates and their imports side by side.

Conventions to follow from day one

  • One component per file, with the file named after the component (user-card.ts, class UserCard).
  • Keep templates short; move long ones into a .html file.
  • Prefer small components that do one thing; they are easier to reuse and test.
  • Use ng generate component instead of creating files by hand so naming stays consistent.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does the selector of a component define?

    With selector 'app-greeting' you place the component by writing the tag app-greeting in a template. Instances are created wherever that tag appears.
  2. 2. You use <app-badge /> in a template but get a "not a known element" error. What is the most likely fix?

    Standalone components list what their template uses in the imports array. A missing import is the usual cause of this error.
  3. 3. What does the @Component decorator mainly do?

    A decorator attaches metadata. The compiler and runtime use it to build and create the component, but the class itself stays a normal TypeScript class.
  4. 4. Since Angular 19, what is true about the standalone setting?

    Components are standalone by default now. You will still see standalone true in older code and standalone false in module-based code.

Summary

  • A component is a TypeScript class plus an HTML template and styles, linked by the @Component decorator.
  • selector is the tag name (kebab-case with a prefix such as app-); template or templateUrl is the view; styles or styleUrl is the CSS.
  • Components are standalone by default; each lists the other components, directives and pipes it uses in imports.
  • Using <app-x /> creates a new instance of the class each time, so components nest into a tree and each copy has its own data.
  • A decorator is metadata the Angular compiler reads; the class itself remains an ordinary class.
  • The stand-ins modeled a component as class plus metadata and showed why a missing import fails.

Next: How Angular Works