What Is a Component
A component is the fundamental building block of Angular. Every piece of your UI is a component. Understanding the anatomy of one component well means understanding how all of them work.
The @Component decorator
Section titled “The @Component decorator”In Angular, a component is a TypeScript class decorated with @Component:
import { Component } from '@angular/core';
@Component({ selector: 'app-movie-card', templateUrl: './movie-card.html', styleUrl: './movie-card.css'})export class MovieCard { // component logic goes here}The @Component decorator tells Angular three things:
selector: The custom HTML tag that represents this component in other templates. When Angular sees<app-movie-card />in a template, it renders this component there.templateUrl: The path to this component’s HTML template. The template defines what the component renders.styleUrl: The path to this component’s styles. These styles are scoped — they only affect elements inside this component’s template.
The selector is a custom HTML element
Section titled “The selector is a custom HTML element”When you include a component in another component’s template, you use its selector as an HTML tag:
<div class="grid"> <app-movie-card /> <app-movie-card /> <app-movie-card /></div>Angular replaces each <app-movie-card /> with that component’s rendered output. The convention is to prefix selectors with app- to distinguish them from standard HTML elements.
The template defines the view
Section titled “The template defines the view”A component’s template is standard HTML, extended with Angular syntax. A minimal MovieCard template might look like:
<div class="card"> <img [src]="posterUrl" [alt]="movie.title" /> <div class="info"> <h3>{{ movie.title }}</h3> <p>{{ movie.release_date | date:'yyyy' }}</p> </div></div>The {{ }} syntax is interpolation — it renders a property value from the component class. The [src] syntax is property binding. The | date is a pipe. You will learn all of these in Module 03.
Scoped styles
Section titled “Scoped styles”Styles in a component’s CSS file are automatically scoped. A rule like:
.card { border-radius: 8px; background: #1a1a1a;}Only applies to .card elements inside this component. Angular adds a unique attribute to the component’s elements and rewrites the CSS to target that attribute. This prevents styles from leaking between components.
Generating a component
Section titled “Generating a component”Use the CLI to generate a component instead of creating files by hand:
ng generate component components/movie-cardShort form:
ng g c components/movie-cardThis creates four files:
src/app/components/movie-card/├── movie-card.ts ← the class├── movie-card.html ← the template├── movie-card.css ← the styles└── movie-card.spec.ts ← the unit testNote: Angular 17+ uses movie-card.ts instead of movie-card.component.ts. The class name is still MovieCard.
Exercise
Section titled “Exercise”- In your
my-first-appproject, generate a component calledcomponents/greeting:Terminal window ng g c components/greeting - Open
greeting.htmland replace its contents with:<p>Hello from GreetingComponent!</p> - Open
app.component.html, clear its contents, and add:<app-greeting /> - Open
app.component.tsand addGreetingto theimportsarray in the@Componentdecorator. (Angular will guide you with an error if you forget.) - Run
ng serveand confirm you see the greeting.
- A component is a TypeScript class decorated with
@Component. - The
selectoris the custom HTML tag used to embed the component in other templates. - The
templateUrlpoints to the component’s HTML template. - The
styleUrlpoints to scoped CSS that only applies inside this component. - Generate components with
ng generate component <path>to create all four files at once.