Binding and Events in Angular

Binding and Events in Angular: Property Binding, Event Binding, Two-Way Binding, @if and @for.

Updated

  1. Property Binding (Apprentice): Square brackets bind an element's property to the component's data, such as [src]="imageUrl" or [disabled]="soldOut". [class.name] turns a CSS class on and off, and [style.prop] sets one style, all kept up to date by Angular.
  2. Event Binding (Apprentice): Parentheses bind an event to a method of the component, such as (click)="addToCart()". $event passes the event itself, a template reference variable such as #box passes an element, and (keyup.enter) listens for one key.
  3. Two-Way Binding (Apprentice): [(ngModel)] binds a form field both ways: the field shows the signal's value, and typing in the field changes the signal. Import FormsModule to use it. Brackets in and parentheses out, together: the banana in a box.
  4. @if and @for (Apprentice): @if shows part of a template only when a condition is true, with @else for the other case. @for repeats part of a template for each item of a list, with track to tell the items apart, and @empty for an empty list.