AI Hints
Home›Angular Tutorial›Components, Templates and Interpolation
Angular · Lesson 19 of 120

Components

The building blocks.

⏱ 2 min read▶ Video at 23:02🎓 Free
Watch this lesson in the Angular courseWatch on YouTube ↗Full Angular course

An Angular app is a tree of components. A header, a book list, each book card, a basket. Every piece of the page is a component.

  • A tree of components
  • Every piece of the page
  • Small and reusable

A page made of components

The root component, App, holds everything. Inside it, there's a header, and a book list. The book list shows a book card for each book. And the basket shows what's been picked. Each component only worries about its own small job.

  1. App (root)
  2. Header
  3. Book list
  4. Book card × many
  5. Basket

What makes a component

A component is a class, with the at Component decorator. The template is the HTML it shows. Styles are the CSS, for this component only. And the selector is the tag that places it on the page.

keyvalue
Classdata and logic
templatethe HTML
stylesCSS, only for this component
selector<app-root>

A complete component

Here's a whole component, in one file. The template has a heading, and a paragraph, with the class note. The styles colour that note in red, and bold. Run it. The heading, and the red note, appear on the page.

app.tstypescript
import { Component } from '@angular/core';

@Component({
  selector: 'app-root',
  template: `
    <h1>The Bookshop</h1>
    <p class="note">Open every day, 9 to 5.</p>
  `,
  styles: `.note { color: #c3002f; font-weight: bold; }`,
})
export class App {}

Summary

Apps are trees of components. Each component has a class, a template, styles, and a selector. Next, templates.

Key takeaways
  • A tree of components
  • @Component
  • template + styles
Next up: Templates — HTML with super powers