Component basics and the @Component decorator

Intermediate · 16 min

What the @Component decorator actually does, and the metadata every component carries.

Before thisThe CLI, project anatomy, and standalone bootstrapping

WebLearns · 16 min

Component basics and the @Component decorator

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by WebLearnsReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • `@Component` is a decorator — it attaches metadata to a plain class, telling Angular how to find, render, and style instances of it.
  • `selector` is just a CSS selector Angular matches against your templates; it doesn't have to be an element name — attribute and class selectors work too.
  • `standalone: true` means the component lists its own template dependencies instead of relying on a module to have declared them for it.

A component is a class first

Strip away everything Angular adds, and a component starts as a plain TypeScript class:

class Counter {
  count = 0;
  increment() {
    this.count++;
  }
}

Nothing here is Angular-specific. It’s a class with a property and a method, the same as any class you’d write in any TypeScript program. The question a framework has to answer is: how does this plain class turn into something rendered on a page, responding to clicks, showing up wherever you write <app-counter> in a template? A class, on its own, has no opinion about HTML. Something has to connect the two.

@Component is that connection, as metadata

@Component({
  selector: 'app-counter',
  standalone: true,
  template: `
    <button (click)="increment()">Count: {{ count }}</button>
  `,
  styleUrl: './counter.component.css',
})
class Counter {
  count = 0;
  increment() {
    this.count++;
  }
}

@Component is a decorator — a function that runs at class-definition time and attaches extra information to the class, without changing what the class itself does. The object you pass it is pure metadata, and each key is answering a specific question Angular needs answered before it can use this class as a component:

  • selector — where does this component go in a template? app-counter means “anywhere you write <app-counter></app-counter>, put an instance of this class here.” Selectors are CSS selectors, not a special mini-language — [appCounter] (an attribute selector) or .app-counter (a class selector) would both be valid, though element selectors are the convention for components.
  • template (or templateUrl for a separate file) — the HTML this component renders, with Angular’s binding syntax ({{ }}, (click)) mixed in. The next lesson covers that binding syntax in depth.
  • styleUrl (or styles inline) — CSS scoped to this component by default; a later lesson on view encapsulation covers exactly how that scoping works and how to intentionally break out of it.
  • standalone: true — this component declares its own dependencies (other components, directives, pipes it uses in its template) rather than relying on an NgModule to have declared it alongside its neighbors. Current-version Angular generates components this way by default.

The class underneath still has count and increment(), completely unchanged. The decorator didn’t touch the class’s behavior — it told Angular how to treat instances of that class as a renderable, selectable thing.

Why metadata instead of inheritance or a base class

An older design for “mark this class as a component” might have you extend a base class — class Counter extends Component. Angular doesn’t do that, and the reason matters: inheritance commits you to one parent, forever, and it mixes framework plumbing into your class’s own method-resolution chain. A decorator sits beside the class instead of inside its inheritance tree — it’s additive metadata the compiler reads, not a contract the class has to structurally satisfy. That’s also why a component’s actual logic (the count/increment part) stays easy to test in isolation: it’s just a class, and the test doesn’t need to fight through framework scaffolding to construct one.

This lesson’s video builds several components from this exact starting point — watch for how little changes between “this is a class” and “this is a component”; the gap is almost entirely the decorator’s metadata, not new class mechanics.

What’s next

You now have a component that renders and reacts to a click. The next lesson goes deeper on the binding syntax inside that template — the difference between {{ count }}, [property]="expr", and the handful of binding forms Angular templates support, and when each one is the right tool.

Check yourself

  1. 01Is `@Component` changing what the class does, or adding information about the class?

    Adding information. The class underneath is a plain TypeScript class with properties and methods — `@Component` attaches metadata (selector, template, styles) that Angular's compiler reads to know how to instantiate and render it. Strip the decorator and the class still compiles; it just isn't a component Angular knows how to use anymore.

  2. 02Could a component's selector be `[appHighlight]` instead of `app-highlight`?

    Yes — a selector is a CSS selector, and `[appHighlight]` matches an attribute rather than an element name. It's unusual for components (that pattern is more common for directives) but nothing stops you; Angular just needs a valid CSS selector to match against your templates.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark