liveBook · Manning

Data binding

As explained in Pro Angular 16

Data binding is a technique that connects application data with a user-interface representation so changes can be reflected between them.

Data binding connects an Angular template to data and logic supplied by its component. A binding contains an expression that Angular evaluates to produce dynamic HTML content. In the one-way bindings described here, values flow from the component into the rendered page. section 11.0 section 11.5

Choose a binding for the part of the element that must change:

  • Use `{{ expression }}` to insert a value into an element’s text, such as `{{ count }}` in a product-count message. section 9.6.2 section 11.5
  • Use a property or attribute binding to configure an element. For example, `[dataSource]="items"` supplies items to an Angular Material table. section 2.5 section 11.0
  • Use class and style bindings when an element’s classes or individual styles depend on application data; `ngClass` and `ngStyle` provide other ways to set them. section 11.0 section 11.5

Bind a component value

A component getter can expose `count`, while its template says `There are {{ count }} products in the model`. When Angular processes the template, it evaluates `count` and creates the displayed HTML using the resulting value. The same pattern works for values prepared from a data model, such as a calculated total. section 9.6.2 section 10.2

Listing 5.16. The contents of the store.component.html file in the src/app/store folder
<div class="container-fluid">
    <div class="row">
        <div class="bg-dark text-white p-2">
            <span class="navbar-brand ml-2">SPORTS STORE</span>
        </div>
    </div>
    <div class="row text-white">
        <div class="col-3 bg-info p-2">
            {{categories().length}} Categories
        </div>
        <div class="col-9 bg-success p-2">
            {{products().length}} Products
        </div>
    </div>
</div>

The store template calls `categories()` and `products()` inside interpolation bindings to display the lengths of component-provided signals. section 5.4.1

Keep binding expressions simple rather than burying logic that cannot readily be tested or reused. A binding has a host element, a target, and an expression. Template expressions are checked during compilation: a reference to a nonexistent component property produces an error. Bindings also take work to evaluate when an Angular page loads, which matters on slower devices. section 11.0 section 11.5 section 2.3.2 section 1.1

How Pro Angular 16 covers it

How deep it goes
Explains one-way bindings through expressions, targets, interpolation, element configuration, classes, and styles; defers advanced bindings and custom extensions.
Examples
Uses product counts, a to-do table’s data source, store-category counts, and cart quantities to illustrate bindings.
What it assumes
Assumes basic HTML familiarity while introducing double-brace expressions and the TypeScript component that supplies their values.
What you can do after
Apply it via Angular templates to display component values and configure element properties, classes, and styles.
How it connects
Links components, templates, change detection, signals, and structural directives through values displayed in HTML.

Also explained in

Compare how 5 books explain data binding →

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.