Skip to the workspace
Pebellslearn by building
Components  ›  Lesson

Anatomy of a Component

Component Anatomy 12 minA component is a TypeScript class marked with the @Component decorator, whose metadata names its selector, template and styles, and whose fields hold the state the template shows.

angular / Anatomy of a Component

Explore each scene at your own pace.

01 · The rule

The rule

Anatomy of a Component

Every screen in Greenhouse, from the whole page down to one small button, is a component. Once you can read one component, you can read them all, because they share the same four parts. This lesson takes one apart.

The rule

A decorator on a class: selector says where, template says what.

import { Component, signal } from '@angular/core';

@Component({
  selector: 'app-plant-card',
  template: `
    <h3>{{ name() }}</h3>
    <p>Water every {{ everyDays() }} days</p>
  `,
  styles: `h3 { color: seagreen; }`,
})
export class PlantCard {
  name = signal('Boston fern');
  everyDays = signal(3);
}
  • The class (PlantCard) holds the component's data and methods. It is normal TypeScript.
  • The @Component decorator attaches metadata: an object that tells Angular "this class is a component, and here is how to show it".
  • selector is the CSS selector Angular matches in parent templates. app-plant-card means "wherever you see <app-plant-card>, put this component". The app- prefix avoids clashes with real HTML tags.
  • template is the HTML it draws; styles is its CSS.

The fields are signals: boxes that hold a value and tell Angular when it changes. You read a signal by calling it, name(), and change it with set or update:

this.everyDays.set(5);
this.everyDays.update(d => d + 1);

Because Angular knows exactly which signals a template reads, a change to everyDays refreshes only what depends on it.

Scene 1 of 5
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.