liveBook · Manning

Component decorator

As explained in Pro Angular 16

The Angular component decorator attaches component metadata to a class so Angular can treat it as a component.

Apply `@Component` to a TypeScript class to tell Angular that the class is a component. The decorator supplies metadata that connects the class to an HTML host element and a template. The class can then provide data and logic for bindings in that template. section 2.3.3 section 5.4.1

The most useful configuration properties are:

  • `selector`: a CSS selector that matches the component’s host element. An element in the HTML must match it. section 16.2.1
  • `template` or `templateUrl`: HTML supplied inline or loaded from a separate file. An external file can make complex content easier to manage. section 5.2.1 section 16.2.1
  • `styles` or `styleUrls`: component-template CSS supplied inline or in separate files. section 16.2.1

Connect a component to its page

A root component with `selector: 'app-root'` matches `<app-root></app-root>` in `index.html`. Its `templateUrl` points to the HTML Angular presents there, while `styleUrls` names its CSS file. A store component can instead use `selector: 'store'` and an external template whose bindings read product and category data from the class. section 2.3.3 section 5.4.1 section 9.4.4

Listing 9.23. The contents of the component.ts file in the src/app folder
import { Component } from "@angular/core";
import { Model } from "./repository.model";
 
@Component({
    selector: "app",
    templateUrl: "template.html"
})
export class ProductComponent {
    private model: Model = new Model();
 
    get count() : number {
      return this.model.getProducts().length;
    }
}

This complete component imports `Component`, attaches a selector and external template to a class, and exposes a getter for the template’s binding. section 9.6.2

The decorator alone does not place a new component on the page in the shown module-based setup. Declare the class in the application’s Angular module, then add an element matching its selector where the component should appear. A component may start with an empty class and a placeholder template; add behavior as its part of the page develops. section 16.2.1

How Pro Angular 16 covers it

How deep it goes
Defines the decorator, explains its common properties, and walks through component setup without detailing Angular’s underlying execution.
Examples
Uses a root element, a store template with product counts, and separate product-table and product-form components.
What it assumes
Assumes familiarity with TypeScript classes and HTML, while defining what the decorator does and how its selector matches an element.
What you can do after
Build it from scratch by decorating a class, selecting its host, supplying a template, and declaring it in an Angular module.
How it connects
Connects CSS selectors, host elements, templates, data binding, component styles, and NgModule declarations.

Also explained in

Compare how 3 books explain component decorator →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Pro Angular 16. The section links point to the parts of the book it draws on.