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
@Componentdecorator attaches metadata: an object that tells Angular "this class is a component, and here is how to show it". selectoris the CSS selector Angular matches in parent templates.app-plant-cardmeans "wherever you see<app-plant-card>, put this component". Theapp-prefix avoids clashes with real HTML tags.templateis the HTML it draws;stylesis 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.