Video lecture
Watch on YouTube(opens in a new tab)Sub topic 1
What a Component Is: Decorator, Selector, Template, and Class
An Angular application is a tree of components. Each component controls one part of the screen. It pairs a TypeScript class, which holds state and behavior, with an HTML template, which describes what to render. This chapter covers the four parts every component has and how Angular connects them.
Prerequisites
This chapter assumes you know:
- HTML and CSS fundamentals
- ES2015+ JavaScript, especially classes and modules
- Basic TypeScript: types, classes, and generics
- How to run npm commands in a terminal
- What the DOM is and how browser events work
The examples target Angular 19 or later, where components are standalone by default.
The four parts
| Part | Role |
|---|---|
| `@Component` decorator | Attaches metadata that turns a plain class into a component |
| `selector` | The CSS selector that tells Angular where to create the component in HTML |
| `template` | The HTML Angular renders for the component |
| Class | Holds the data and methods the template uses |
A minimal component
import { Component, signal } from '@angular/core';
@Component({
selector: 'app-counter',
template: `
<p>Count: {{ count() }}</p>
<button (click)="increment()">Add one</button>
`,
styles: `p { font-weight: bold; }`
})
export class CounterComponent {
count = signal(0);
increment() {
this.count.update(c => c + 1);
}
}Here is what each piece does:
- Decorator.
@Component({...})registers the class with Angular's compiler. Without it,CounterComponentis just a class. - Selector.
app-countermatches the element<app-counter />. When Angular finds that element in another template, it creates an instance of the class and renders the template inside the element. That element is called the host element. - Template. The template is HTML with Angular syntax.
{{ count() }}displays a value and(click)handles an event. Later chapters cover both in detail. - Class. Every public property and method on the class is available to the template. The template has no access to anything outside its component instance.
Inline versus external files
For larger components, you can move the template and styles into separate files:
import { Component } from '@angular/core';
@Component({
selector: 'app-profile',
templateUrl: './profile.component.html',
styleUrl: './profile.component.css'
})
export class ProfileComponent {}Use template or templateUrl, never both. The Angular CLI command ng generate component profile creates this layout for you.
Common errors
- Unknown element error. You used
<app-counter />in another component but did not addCounterComponentto that component'simports. The next chapter covers this. - Template calls a missing member. The compiler checks that template names exist on the class. A typo such as
{{ cuont() }}fails at build time. - Private members in templates. Angular cannot reach
privatemembers from the template. Useprotectedor public members for anything the template reads.
Exercise
You write a component with selector 'app-banner' and the template is never rendered, even though the class compiles. You placed <banner></banner> in a parent template. What is wrong?
Show answer
The selector is 'app-banner', so Angular only matches the element <app-banner>. The <banner> element matches nothing. Change it to <app-banner /> and make sure BannerComponent is listed in the parent's imports array.
Exercise
Which decorator property would you use to keep a component's HTML in its own file, and can it be combined with the template property?
Show answer
Use templateUrl, for example templateUrl: './banner.component.html'. You cannot combine it with template. A component has exactly one template source.