liveBook · Manning

Unidirectional data pipeline

As explained in Pro Angular 16

One-way data binding is a technique in which data flows from an application value or source to a user-interface representation in a single direction.

A one-way data binding carries a value from an Angular component into a template. Angular evaluates an expression in the component’s context, then uses the result to change an HTML element’s content or appearance. This is the component-to-interface direction of data flow; it does not, by itself, handle user input. section 11.2

Listing 11.3. Appling a data binding in the template.html file in the src/app folder
<div [ngClass]="classes()" >
    Hello, World.
</div>

The div is the host element. Angular evaluates classes() and passes its result to ngClass, which sets the element’s CSS class membership. section 11.2

Read a bracketed binding as four parts:

  • The host element is the HTML element being changed. section 11.2
  • Square brackets tell Angular to evaluate the expression; the target, such as ngClass, determines what the result changes. section 11.2
  • The expression can read properties and methods from the template’s component, including a signal value such as classes(). section 11.2

When the brackets are missing

Compare [ngClass]="'text-white p-2 ' + classes()" with ngClass="'text-white p-2 ' + classes()". In the second form, Angular passes the quoted content as a literal rather than evaluating it. If the styling looks wrong, check the brackets and inspect the resulting class attribute in the browser. section 11.2.3

Keep one-way binding expressions idempotent: repeated evaluation must not change application state. An assignment in a binding produces a parser error; reading a property that removes a product from an array also causes trouble. Use expressions to produce display values, not to consume or modify data. section 12.3.1

For user interaction, an event binding runs in the opposite direction, from an element into the application. A mouseover handler can update a selected-product signal; interpolation can then display the selection, while a one-way class binding highlights its table row. Two-way binding is a separate option used most often in forms. section 13.2 section 11.2

How Pro Angular 16 covers it

How deep it goes
Explains binding anatomy, expression evaluation, bracket pitfalls, and idempotence; treats event and two-way bindings separately.
Examples
Uses an ngClass div, an omitted-brackets comparison, invalid state-changing expressions, and an interactive product table.
What it assumes
Introduces binding parts from a div example while relying on HTML elements and component properties or methods for context.
What you can do after
Apply it via Angular template bindings to display component values and diagnose literal-value or state-changing expression mistakes.
How it connects
Connects one-way data binding to Angular templates, directives, signals, string interpolation, event binding, and two-way binding.

Also explained in

Compare how 2 books explain unidirectional data pipeline →

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.