liveBook · Manning

Component

As explained in Pro Angular 16

A component is a modular application building block that encapsulates a portion of user-interface structure and behavior.

An Angular component is a class that manages its own template. The class supplies data and logic; the template defines the HTML content and uses bindings to display values or respond to events. Unlike a general-purpose directive, a component is closely tied to the content it produces. Splitting an application into components keeps features manageable, reusable, and easier to test in isolation. section 16.0 section 16.2

To add a component:

  • Create a TypeScript class and apply the @Component decorator. Give it a selector and an inline template or a templateUrl for an external HTML file. section 16.2.1 section 5.2.1
  • Declare the class in the application’s Angular module, then place an element matching its selector in a template. That element becomes the component’s host. section 16.2.1
  • Put display data and logic in the class, and bind to its properties or methods from the template. For example, a count getter can read a model while {{ count }} displays the result. section 9.6.2
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 pairs a model-backed count getter with a selector and an external template; the matching template displays {{ count }}. section 9.6.2

Every Angular application has a root component for top-level content. Leaving the form, table, layout, and their state in that one component makes the application harder to maintain and test. Separate form and table components can take responsibility for their own content instead. section 16.2 section 16.2.1

Components can coordinate through input and output properties, define CSS styles limited to their templates, and query generated content. They can also receive services: a message component obtains messages from a service and displays the latest one through a computed signal. To test behavior that depends on a host, template, or service, use Angular’s test bed rather than testing the class alone. section 16.5 section 20.3.2 section 29.0

How Pro Angular 16 covers it

How deep it goes
Works through class creation, decorator configuration, module declaration, host placement, component coordination, styling, and testing.
Examples
Uses a model-backed product count, separate product form and table components, and a service-backed message display.
What it assumes
Assumes familiarity with TypeScript classes, imports, and HTML while showing how decorators, selectors, templates, and module declarations fit together.
What you can do after
Build it from scratch: define and register a component, attach it to a host element, and move a UI feature into its template.
How it connects
Connects components to directives, templates, data binding, Angular modules, input and output properties, services, and unit testing.

Also explained in

Compare how 10 books explain component →

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.