liveBook · Manning

Decorator

As explained in Pro Angular 16

A decorator is a language construct that attaches metadata or behavior to a class or other declaration.

A decorator attaches metadata to a class or other declaration. Angular’s @Component decorator identifies a class as a component and supplies configuration for its behavior and generated content. section 2.3.3

The @Component configuration connects the class to the page through three properties:

  • selector matches the HTML element where the component’s generated content is introduced. section 2.3.3
  • templateUrl identifies the template used for the component. section 2.3.3
  • styleUrls identifies the component’s stylesheets. section 2.3.3

Configuring AppComponent

The decorator below declares AppComponent as the app-root component and connects it to its template and styles: @Component({ selector: 'app-root', templateUrl: './app.component.html', styleUrls: ['./app.component.css'] }). The matching <app-root> element in index.html receives the application content. section 2.3.3

Listing 2.10. Editing the contents of the app.component.ts file in the src/app folder
import { Component } from '@angular/core';
import { TodoList } from "./todoList";
import { TodoItem } from "./todoItem";

@Component({
    selector: 'app-root',
    templateUrl: './app.component.html',
    styleUrls: ['./app.component.css']
})
export class AppComponent {
    private list = new TodoList("Bob", [
        new TodoItem("Go for run", true),
        new TodoItem("Get flowers"),
        new TodoItem("Collect tickets"),
    ]);

    get username(): string {
        return this.list.user;
    }

    get itemCount(): number {
        return this.list.items
            .filter(item => !item.complete).length;
    }
}

Inspect the complete component file, including imports, decorator configuration, model data, and component properties. section 2.3.3

How Pro Angular 16 covers it

How deep it goes
Defines metadata properties and traces selector matching to a custom element, while deferring further runtime details.
Examples
Uses AppComponent with app-root, app.component.html, and app.component.css in a todo application.
What it assumes
Assumes classes and imports, then teaches the selector-to-element relationship while skipping deeper execution details.
What you can do after
Apply it via Angular to configure a component class and connect it to an HTML element.
How it connects
Connects classes with metadata, Angular components, CSS selectors, HTML elements, templates, and stylesheets.

Also explained in

Compare how 7 books explain 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.