Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
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.

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.

The shortcut

Read any component top to bottom as a sentence: "When a parent writes selector, draw template, styled by styles, using data from class." If one of those four is missing or wrong, you have found your bug.

Reading WaterButtonStep 1 of 4
  1. The selector

    selector: 'app-water-button' — a parent will write <app-water-button />.

The usual slip is writing {{ name }} instead of {{ name() }}. Without the call you are showing the signal itself, not its value, so the page prints something like [Signal: Boston fern] instead of plain "Boston fern".

Drills

Answer fast — trust the rule.

quick drill

What does the selector in @Component decide?

quick drill

Given name = signal('Fern'), how does the template show the value?

quick drill

Which call adds one to everyDays = signal(3), based on its current value?

quick drill

What does the @Component decorator add to the class?

Remember

  • A component is a class plus @Component metadata: selector, template, styles.
  • Read it as one sentence: where a parent writes the selector, draw the template using the class's data.
  • Trap: signals are read by calling them, name(), not name.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainRead a component aloud

Write a small PlantCard component for Greenhouse that shows a plant name held in a signal, then explain what the decorator, the selector, the template and the class each contribute.