liveBook · Manning

Selector property

As explained in Essential TypeScript 5, Third Edition

A selector property is Angular component metadata that specifies the element or attribute pattern identifying where the component is used in a template.

A selector property tells Angular where a component belongs in an application’s HTML. It defines the CSS selector used to identify the component’s element or attribute pattern. section 18.4

For ProductItem, selector: "product-item" connects the ProductItem class with product-item elements. Angular then uses the component’s templateUrl to render the matching template. section 18.4

Matching a component in a template

In src/app/productItem.component.ts: @Component({ selector: "product-item", templateUrl: "./productItem.component.html" }) export class ProductItem { } A parent template can then use: <product-item [product]="p" (addToCart)="handleAdd($event)"></product-item> The selector chooses the component; the surrounding attributes and event binding pass data into it and receive notifications from it.

Selectors also connect several components into a larger template. ProductList uses header, category-list, and product-item elements, each corresponding to another component’s selector property. section 18.4.3

Angular resolves these elements only among components configured through the Angular module. The module declares the component classes, while app.component.html begins the composition with <product-list></product-list>. section 18.5

How Essential TypeScript 5, Third Edition covers it

How deep it goes
Defines CSS-selector matching and follows resolution through nested templates, but stops short of selector syntax variations.
Examples
Uses product-item, product-list, header, and category-list elements in product-list.component.html and app.component.html.
What it assumes
Assumes familiarity with Angular components and HTML templates while demonstrating component registration and template composition.
What you can do after
Apply it via Angular by assigning a selector and placing the matching element in a declared component template.
How it connects
Connects Angular components, templates, NgModule declarations, templateUrl, Input, and Output.

Also explained in

Compare how 2 books explain selector property →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Essential TypeScript 5, Third Edition. The section links point to the parts of the book it draws on.