Blog

Part 2 · Templates and Bindings in Angular: {{ }}, [property] and (event)

A template connects your component’s data to the page. This part covers interpolation, property, attribute, class and style bindings, event bindings with $event, and template reference variables.

A component has two halves. The class holds data and methods. The template is the HTML the user sees. Bindings are how the two talk: data goes from the class into the template, and events come back from the page into the class.

This part covers the five kinds of binding you’ll write every day: double curly braces for text, [property], [attr.name], [class.name] and [style.name] for element properties, and (event) for clicks and typing. We also cover $event and template reference variables. In Part 1 we made a component with an inline template. Here we fill that template with live values. Every example here is a whole file you can paste into your practice project from Part 1. Each one was built with Angular 22.3 and run in a browser before this was published.

Try this first

Paste this into src/app/app.ts:

// src/app/app.ts
import { Component } from '@angular/core';

@Component({
  selector: 'app-root',
  template: `
    <p>{{ name }} has {{ points }} points.</p>
    <p>Next level at {{ points * 2 }}. Go, {{ name.toUpperCase() }}!</p>
    <button disabled="false">Save</button>
  `,
})
export class App {
  protected readonly name = 'Ana';
  protected readonly points = 40;
}

protected keeps a field out of the class’s public API, but its template can still use it. That’s the modern style for members only the template needs. readonly means the field is never given a new value. We mark fields that never change readonly, and leave it off the ones an event changes.

Before you read on, guess. What do the two lines of text say? And can you click Save?

The page shows:

Ana has 40 points.
Next level at 80. Go, ANA!
Save

The text is what you’d expect. The button is the surprise: it’s greyed out, and clicking it does nothing. The rendered HTML is:

<button disabled="false">Save</button>

disabled="false" is a plain HTML attribute. For disabled, the browser only checks whether the attribute is there. Its value doesn’t matter, so even "false" disables the button. To set it from a value, you need a property binding, and we’ll get there in a moment.

Interpolation: double curly braces

Double curly braces put a value into the text of the page. Angular calls this interpolation. Between the braces goes a template expression: a small piece of code that Angular runs against the component. name in the template means this.name in the class. You don’t write this.

A template expression can do most of what a JavaScript expression can:

// src/app/app.ts
import { Component } from '@angular/core';

interface Order {
  item: string;
}

@Component({
  selector: 'app-root',
  template: `
    <p>{{ points >= 50 ? 'Gold' : 'Silver' }} member</p>
    <p>{{ greet('Ben') }}</p>
    <p>Last order: {{ lastOrder?.item ?? 'none' }}</p>
    <p>{{ note }}</p>
  `,
})
export class App {
  protected readonly points = 40;
  protected readonly lastOrder: Order | null = null;
  protected readonly note = '<b>Bold?</b>';

  protected greet(name: string) {
    return `Hello, ${name}`;
  }
}

The page shows:

Silver member
Hello, Ben
Last order: none
<b>Bold?</b>

Line by line:

  • condition ? a : b is JavaScript’s short if. It gives a when the condition is true and b when it’s false.
  • greet('Ben') calls a method on the class. The method returns a string, and the string is shown.
  • lastOrder?.item reads item only if lastOrder isn’t null. Otherwise it gives undefined, and ?? 'none' swaps that for 'none'. The type Order | null means “an Order, or null“. TypeScript checks templates too, so without the ?. the build would stop with TS2531, Object is possibly 'null'.
  • note holds the text <b>Bold?</b>. Interpolation always shows text. Angular doesn’t turn it into HTML, so the tags show up as written. That’s a safety feature: text typed by a user can’t sneak a <script> into your page.

What a template expression can’t do

Template expressions are for reading values. Two things you might try don’t build:

// src/app/app.ts
import { Component } from '@angular/core';

@Component({
  selector: 'app-root',
  template: `
    <p>{{ points = 100 }}</p>
    <p>Average: {{ Math.round(average) }}</p>
  `,
})
export class App {
  protected readonly points = 40;
  protected readonly average = 7.6;
}

ng serve stops with these errors (among the lines around them):

✘ [ERROR] TS2339: Property 'Math' does not exist on type 'App'. [plugin angular-compiler]
...
✘ [ERROR] NG5002: Parser Error: Bindings cannot contain assignments at column 11 in [ points = 100 ] in

The NG5002 line ends with the file path, cut off here. It says what you’d hope: an interpolation can’t change anything. =, ++ and += are all refused. A template that changed data while it was being drawn would be very hard to follow.

The TS2339 error is less obvious. A template expression can only see the component’s own members, so Math means this.Math, and App has no Math. The same goes for Date, JSON, window and console. The fix is to do that work in the class:

// src/app/app.ts
import { Component } from '@angular/core';

@Component({
  selector: 'app-root',
  template: `<p>Average: {{ rounded() }}</p>`,
})
export class App {
  protected readonly average = 7.6;

  protected rounded() {
    return Math.round(this.average);
  }
}

The page shows:

Average: 8

new is refused too ({{ new Date() }} is a parser error). Keep template expressions short: read a value, call a method, maybe a ? :. Anything longer belongs in the class, where it has a name and you can test it.

Property binding: [property]

Square brackets set a DOM property on an element from a template expression. Back to the Save button:

// src/app/app.ts
import { Component } from '@angular/core';

@Component({
  selector: 'app-root',
  template: `
    <img [src]="photo" [alt]="name + ', smiling'" width="40" height="40" />
    <p>{{ name }}</p>
    <button [disabled]="saving">Save</button>
    <button [disabled]="!saving">Cancel</button>
  `,
})
export class App {
  protected readonly name = 'Ana';
  protected readonly photo = '/ana.svg';
  protected readonly saving = false;
}

The image is a file in your project’s public/ folder. Angular serves that folder at the root of the site, so public/ana.svg is /ana.svg. Save this as public/ana.svg, or point photo at any image you have. ng serve only picks up new files in public/ when it starts, so stop it (Ctrl+C) and run it again after adding one:

<svg xmlns="http://www.w3.org/2000/svg" width="40" height="40"><circle cx="20" cy="20" r="18" fill="peru"/></svg>

The page shows a brown circle, the name, and the two buttons:

Ana
SaveCancel

The two buttons show as SaveCancel because buttons sit on one line, and the page text runs them together. The HTML Angular made is:

<img width="40" height="40" src="/ana.svg" alt="Ana, smiling" />
<p>Ana</p>
<button>Save</button>
<button disabled="">Cancel</button>

The quotes after [disabled]= hold a template expression, not text. saving is false, so Save’s disabled property is false and the button works. !saving is true, so Cancel is disabled. [alt]="name + ', smiling'" builds a string, with the text part in single quotes.

Without the brackets, src="photo" would be the plain text photo. With them, it’s the value of the photo field. That’s the whole difference, and it’s the most common slip in a new template.

Properties and attributes are not the same thing

HTML attributes are what you write in the HTML file. When the browser reads the page, it makes a DOM element for each tag: a JavaScript object with properties. Many attributes have a matching property, so they look the same, but they differ:

  • an attribute is text. For some, like value on an <input> or checked, it only sets the starting value, and the property then changes as the user types or clicks;
  • a property is the live value, and it can be any type: disabled is a boolean, colSpan is a number.

[x] in Angular sets the property x. Usually the names match. Sometimes they don’t. A table cell has the attribute colspan, but its property is colSpan, with a capital S:

// src/app/app.ts
import { Component } from '@angular/core';

@Component({
  selector: 'app-root',
  template: `
    <table>
      <tr>
        <td>Apples</td>
        <td>2</td>
      </tr>
      <tr>
        <td [colspan]="columns">Total: 2</td>
      </tr>
    </table>
  `,
})
export class App {
  protected readonly columns = 2;
}

The build stops:

✘ [ERROR] NG8002: Can't bind to 'colspan' since it isn't a known property of 'td'. Find more at https://next.angular.dev/errors/NG8002 [plugin angular-compiler]

Angular keeps a list of the properties of standard HTML elements, so it can tell you at build time that td has no property called colspan. The fix is the property’s real name: [colSpan]="columns". The links in Angular’s error messages may point at next.angular.dev, the docs for the next version; the same page on angular.dev is the one for your version.

Attribute binding: [attr.name]

[attr.name] sets an HTML attribute from an expression. You need it for attributes that have no property at all, such as your own data-* attributes. This series also uses it for every ARIA attribute (the aria-* attributes that tell screen readers what an element is or does). Those do have properties, like ariaLabel, but [attr.aria-label] always sets exactly the attribute you name, so it’s easy to read and to check:

// src/app/app.ts
import { Component } from '@angular/core';

@Component({
  selector: 'app-root',
  template: `
    <table>
      <tr>
        <td>Apples</td>
        <td>2</td>
      </tr>
      <tr>
        <td [colSpan]="columns">Total: 2</td>
      </tr>
    </table>
    <button [attr.aria-label]="'Remove ' + item">×</button>
    <p [attr.data-note]="note">Hover me</p>
  `,
})
export class App {
  protected readonly columns = 2;
  protected readonly item = 'Apples';
  protected readonly note: string | null = null;
}

The page shows:

Apples	2
Total: 2
×
Hover me

The tab between Apples and 2 is the table’s two cells. The HTML is below. The browser keeps the colspan attribute in step with the colSpan property, so it shows up too:

<td colspan="2">Total: 2</td>
<button aria-label="Remove Apples">×</button>
<p>Hover me</p>

A screen reader says “Remove Apples, button” instead of “times, button”. And note is null, so the <p> has no data-note at all: binding an attribute to null (or undefined) removes it. That’s handy for attributes you only sometimes want.

You’ll also see [aria-label]="..." without attr. in newer code. Angular 22 accepts it and sets the attribute. [attr.aria-label] works for every attribute, so this series uses that.

Class and style bindings

Two more bindings set CSS. They’re small, so here they are together:

// src/app/app.ts
import { Component } from '@angular/core';

@Component({
  selector: 'app-root',
  template: `
    <p [class.done]="progress === 100" [class]="size">Upload</p>
    <div [style.width.px]="progress * 2" [style.background-color]="barColor">{{ progress }}%</div>
    <p [class]="{ warn: progress < 50, big: true }">Status</p>
  `,
  styles: `
    .done {
      text-decoration: line-through;
    }
  `,
})
export class App {
  protected readonly progress = 100;
  protected readonly size = 'big';
  protected readonly barColor = 'lightgreen';
}

styles is CSS for this component only. Part 7 covers it; for now, read it as a <style> tag that only applies to this template.

The page shows:

Upload
100%
Status

and the HTML is:

<p class="big done">Upload</p>
<div style="width: 200px; background-color: lightgreen;">100%</div>
<p class="big">Status</p>
  • [class.done]="expr" adds the class done when expr is true and removes it when it’s false. Other classes on the element are left alone.
  • [class]="..." takes a string of class names ('big', or 'big bold'), or an object whose keys are class names and whose values say on or off. { warn: progress < 50, big: true } gives just big. It can sit next to [class.done], and the two are combined.
  • [style.width.px]="progress * 2" sets one style. The .px on the end is the unit, so a number becomes 200px. .em, .% and the other CSS units work the same way.
  • [style.background-color] takes the CSS name of the style, with its dash.

Reach for [class.x] first. A class keeps the looks in CSS, where they belong, and the template only says when. Use [style.x] for values you work out, like a width.

Event binding: (event)

Round brackets go the other way. (click)="..." runs code when the event happens:

// src/app/app.ts
import { Component } from '@angular/core';

@Component({
  selector: 'app-root',
  template: `
    <p>Clicked {{ clicks }} times</p>
    <button (click)="clicks = clicks + 1">Add one</button>
    <button (click)="reset()">Reset</button>
  `,
})
export class App {
  protected clicks = 0;

  protected reset() {
    this.clicks = 0;
  }
}

Click Add one three times. The page shows:

Clicked 3 times
Add oneReset

Click Reset, and it shows:

Clicked 0 times
Add oneReset

The code in an event binding is a template statement. Unlike an interpolation, it can assign: clicks = clicks + 1 is allowed here, because changing things is what events are for. Still, keep it to one short statement or a method call. reset() has a name, and it’s easy to find later.

The screen updated because Angular redraws a component after an event in its template. Every example in this part changes a plain field from an event, so that’s enough for now. Part 3 shows where plain fields stop working, and how signals fix it.

$event: what happened

Inside an event binding, $event is the browser’s event object. For typing, that’s how you get the text:

// src/app/app.ts
import { Component } from '@angular/core';

@Component({
  selector: 'app-root',
  template: `
    <label>Name <input (input)="draft = $event.target.value" (keyup.enter)="save()" /></label>
    <p>Typing: {{ draft }}</p>
    <p>Saved: {{ saved }}</p>
  `,
})
export class App {
  protected draft = '';
  protected saved = '';

  protected save() {
    this.saved = this.draft;
  }
}

Type Ana into the field. The page shows:

Name
Typing: Ana
Saved:

Now press Enter:

Name
Typing: Ana
Saved: Ana

Three things happen here:

  • (input) fires on every change to the field’s text.
  • $event.target is the element the event came from, and .value is its current text.
  • (keyup.enter) fires when the Enter key comes back up. It’s Angular’s shortcut for “a keyup event, but only for Enter”. (keydown.escape), (keyup.shift.enter) and others work the same way.

TypeScript knows the type of $event. For (input) it’s an InputEvent, for (click) a PointerEvent, for (keyup) a KeyboardEvent. And on an <input>, Angular 22 types $event.target as HTMLInputElement, so .value is a string and the build is happy.

That last part doesn’t hold for every element. Change the <input> to a <textarea>:

// src/app/app.ts
import { Component } from '@angular/core';

@Component({
  selector: 'app-root',
  template: `
    <label>Notes <textarea (input)="notes = $event.target.value"></textarea></label>
    <p>{{ notes.length }} characters</p>
  `,
})
export class App {
  protected notes = '';
}

The build stops:

✘ [ERROR] TS2531: Object is possibly 'null'. [plugin angular-compiler]
...
✘ [ERROR] TS2339: Property 'value' does not exist on type 'EventTarget'. [plugin angular-compiler]

Angular 22 gives $event.target the exact element type only on void elements, the ones with no closing tag, like <input> and <img>. A <textarea> has a closing tag, so it doesn’t get this, and neither do <select>, <button> or <div>. There $event.target has the general type EventTarget | null. It might be null, and a plain EventTarget has no value. Those are the two errors.

Template reference variables: #name

The clean fix is to name the element and read it directly. #notesBox on an element makes a template reference variable: a name the rest of the template can use for that element.

// src/app/app.ts
import { Component } from '@angular/core';

@Component({
  selector: 'app-root',
  template: `
    <label>Notes <textarea #notesBox (input)="notes = notesBox.value"></textarea></label>
    <p>{{ notes.length }} characters</p>
  `,
})
export class App {
  protected notes = '';
}

Type Buy milk into the box. The page shows:

Notes
8 characters

notesBox has the type HTMLTextAreaElement, so notesBox.value is a typed string. Nothing is cast, and nothing is switched off.

A reference variable works anywhere in the same template block, not only on its own element. (A block is a part like the inside of an @if, which Part 4 covers; a name made inside one isn’t visible outside it.) A button can read a field next to it, or move the cursor into it with focus():

// src/app/app.ts
import { Component } from '@angular/core';

@Component({
  selector: 'app-root',
  template: `
    <input #item aria-label="Item" (keyup.enter)="add(item.value); item.value = ''" />
    <button (click)="add(item.value); item.value = ''; item.focus()">Add</button>
    <p>List: {{ items.join(', ') }}</p>
  `,
})
export class App {
  protected items: string[] = [];

  protected add(text: string) {
    this.items = [...this.items, text];
  }
}

Type milk and press Enter, then type eggs and click Add. The page shows:

Add
List: milk, eggs

A statement can hold several steps, split by ;. Here we add the item, clear the field, and put the cursor back in it. string[] is the TypeScript type “a list of strings”. [...this.items, text] makes a new list with everything from the old one, then text. Part 3 explains why a new list is a good habit.

The picture so far

Every binding in this part goes one of two ways:

component class (data) template, as it ends up in the DOM name = 'Ana' saving = true liked = true toggleLike() <p>Ana</p> <button disabled> <button class="liked"> Like button {{ name }} [disabled]="saving" [class.liked]="liked" (click)="toggleLike()" data in: {{ }}, [property], [attr.x], [class.x], [style.x] events out: (event) runs code in the class

Data flows from the class into the template. Events flow from the page back into the class.

The solid black arrows are data going in: the class has a value, and the template shows it as text, a property, an attribute, a class or a style. The dashed red arrow is an event coming out: something happens on the page, and the template calls code in the class. That code changes a field, and the black arrows carry the new value back in on the next redraw.

The brackets match the direction. [ ] puts a value in. ( ) lets an event out. You’ll meet [( )], both at once, in Part 6.

Worked example: a like button and a details panel

Now let’s put these together. A post has a Like button that toggles, shows the count, and changes colour. A Details button opens and closes a small panel.

// src/app/app.ts
import { Component } from '@angular/core';

@Component({
  selector: 'app-root',
  template: `
    <button [class.liked]="liked" [attr.aria-pressed]="liked" (click)="toggleLike()">
      {{ liked ? 'Liked' : 'Like' }} ({{ likes }})
    </button>
    <button [attr.aria-expanded]="open" (click)="open = !open">Details</button>
    <div [hidden]="!open" [style.max-width.px]="300">
      <p>{{ likes }} people liked this post.</p>
    </div>
  `,
  styles: `
    .liked {
      color: crimson;
      font-weight: bold;
    }
  `,
})
export class App {
  protected liked = false;
  protected likes = 12;
  protected open = false;

  protected toggleLike() {
    this.liked = !this.liked;
    this.likes += this.liked ? 1 : -1;
  }
}

What each binding does:

  • [class.liked]="liked" turns the button red and bold while it’s liked.
  • [attr.aria-pressed]="liked" tells a screen reader this is a toggle button and whether it’s on. Like every ARIA attribute in this series, it’s bound with attr..
  • {{ liked ? 'Liked' : 'Like' }} changes the label.
  • (click)="toggleLike()" flips liked and moves the count up or down by one.
  • [hidden]="!open" binds the hidden property. A hidden element isn’t shown at all.
  • (click)="open = !open" flips open. [attr.aria-expanded] tells a screen reader whether the panel is open.

When the page loads, it shows:

Like (12)Details

The panel is hidden, so its text isn’t there. Click Like (12), then Details. The page shows:

Liked (13)Details
13 people liked this post.

and the button now has both the class and the ARIA attribute:

<button aria-pressed="true" class="liked">Liked (13)</button>

Click Liked (13) to undo it. The page shows:

Like (12)Details
12 people liked this post.

When liked is false, [class.liked] removes the class (an empty class="" is left behind, which does nothing), and aria-pressed becomes "false". The boolean is turned into text, which is exactly what aria-pressed expects.

Worked example: reading a build error

You’re adding a Saving… state to a form, and you mistype one property name:

// src/app/app.ts
import { Component } from '@angular/core';

@Component({
  selector: 'app-root',
  template: `
    <button [disabeld]="saving" (click)="save()">Save</button>
    <p>{{ saving ? 'Saving...' : 'Ready' }}</p>
  `,
})
export class App {
  protected saving = false;

  protected save() {
    this.saving = true;
  }
}

ng serve refuses to build it:

✘ [ERROR] NG8002: Can't bind to 'disabeld' since it isn't a known property of 'button'. Find more at https://next.angular.dev/errors/NG8002 [plugin angular-compiler]

    src/app/app.ts:7:12:
      7 │     <button [disabeld]="saving" (click)="save()">Save</button>
        ╵             ~~~~~~~~~~~~~~~~~~~

Read it piece by piece:

  • NG8002 is an Angular error code. Angular’s codes start with NG; TypeScript’s start with TS. The link at the end of the line explains the code.
  • Can't bind to 'disabeld' since it isn't a known property of 'button' names the binding and the element. Angular checked disabeld against everything a <button> has, and found nothing.
  • src/app/app.ts:7:12 is the file, the line and the column. The ~~~ underlines the whole binding. (The real output starts the path with your project’s folder.) The line number counts from the top of app.ts, not from the top of the template.

So the fix is the spelling. Without the brackets, a typo like disabeld="true" would be a plain attribute the browser ignores, and you’d never be told. That’s one more reason to bind with [ ]: Angular checks the name for you.

// src/app/app.ts
import { Component } from '@angular/core';

@Component({
  selector: 'app-root',
  template: `
    <button [disabled]="saving" (click)="save()">Save</button>
    <p>{{ saving ? 'Saving...' : 'Ready' }}</p>
  `,
})
export class App {
  protected saving = false;

  protected save() {
    this.saving = true;
  }
}

Click Save. The page shows:

Save
Saving...

and the button is now disabled.

You’ll meet NG8002 again in Part 5, on your own components. If you use a component but forget to add it to imports, you get NG8002 for each binding on it, next to an NG8001 error that says the element isn’t known. Either way, ask the same question: does this element really have a property with this exact name?

In older code

Older templates often use [ngClass]="{ ... }" and [ngStyle]="{ ... }" from CommonModule. They do the same job as [class] and [style], which need no import, so new code uses the built-in bindings. You’ll also see $any($event.target).value. $any() switches off type checking for that expression, so a typo like .valeu builds without a word and gives undefined at run time. A template reference variable gives you the same value with the types left on.

Best practices

  • Bind with [ ] whenever the value isn’t fixed text. [disabled]="saving" sets a real boolean, and Angular checks the property name at build time. It replaces disabled="{{ saving }}" and hand-written attributes that the browser reads its own way.
  • Keep template expressions short. Read a value, call a method, maybe a ? :. Move anything longer into a method in the class, where it has a name and can be tested.
  • Use [attr.x] for attributes with no property (like data-*) and for ARIA attributes. For disabled, src, value, hidden and colSpan, bind the property. It replaces guessing which form works.
  • Prefer [class.x] to [style.x]. The template says when, and the CSS says how it looks. It replaces long inline styles that are hard to change later.
  • Read form fields with a template reference variable when $event.target isn’t typed. #box and box.value keep full type checking. They replace $any($event.target).value.

Tips and traps

  • disabled="false" disables the button. A plain attribute is present or not, whatever its value. Use [disabled]="false".
  • Forgetting the brackets. src="photo" asks the browser for a file named photo, and [src]="photo" uses the field. A missing image or a literal field name on the page is the clue.
  • Binding a property that doesn’t exist. [colspan] and typos like [disabeld] fail with NG8002. Check the spelling, and check the property’s real name: it’s colSpan, with a capital S.
  • Using globals in a template. {{ Math.round(x) }} fails with TS2339, Property 'Math' does not exist on type 'App'. Put a method in the class.
  • Assigning in an interpolation. {{ count = 5 }} and {{ count++ }} fail with NG5002. Change data in an event binding or a method, not while drawing.
  • $event.target.value on a <textarea> or <select>. It fails with TS2531 and TS2339. Name the element with #box and read box.value.

Square brackets carry data into the template, round brackets carry events out, and Angular checks both when it builds.

Exercises

Exercise 1. This button should be enabled while blocked is false. Paste it. The button is disabled anyway. Why? Fix it.

// src/app/app.ts
import { Component } from '@angular/core';

@Component({
  selector: 'app-root',
  template: `<button disabled="{{ blocked }}">Send</button>`,
})
export class App {
  protected readonly blocked = false;
}

Exercise 2. Write a component with a text field labelled Tweet and a line underneath that says how many characters are typed, as 5 / 20. When there are more than 20 characters, the line gets the class over, which shows it in red. Type Hello and the page should show 5 / 20.

Exercise 3. Without running it, which lines of this template stop the build, and with which error codes?

// src/app/app.ts
import { Component } from '@angular/core';

@Component({
  selector: 'app-root',
  template: `
    <p>{{ total += 1 }}</p>
    <td [rowspan]="2">A</td>
    <button (click)="total = total + 1">Add</button>
    <p [attr.data-total]="total">{{ total > 3 ? 'many' : 'few' }}</p>
  `,
})
export class App {
  protected total = 0;
}
Answers

1. disabled="{{ blocked }}" turns the value into a string, so it sets the disabled property to the text "false". Any non-empty string counts as true, so the button is disabled. Bind the property instead, so it gets the boolean:

// src/app/app.ts
import { Component } from '@angular/core';

@Component({
  selector: 'app-root',
  template: `<button [disabled]="blocked">Send</button>`,
})
export class App {
  protected readonly blocked = false;
}

Now the HTML has no disabled at all:

<button>Send</button>

2. Read the text with $event.target.value (it’s an <input>, so it’s typed) and add the class with [class.over]:

// src/app/app.ts
import { Component } from '@angular/core';

@Component({
  selector: 'app-root',
  template: `
    <label>Tweet <input (input)="text = $event.target.value" /></label>
    <p [class.over]="text.length > limit">{{ text.length }} / {{ limit }}</p>
  `,
  styles: `
    .over {
      color: red;
    }
  `,
})
export class App {
  protected text = '';
  protected readonly limit = 20;
}

Type Hello. The page shows:

Tweet
5 / 20

Now type This is far too long for it. The page shows:

Tweet
27 / 20

and the line has the class over, so it’s red.

3. Two lines fail. {{ total += 1 }} fails with NG5002, because an interpolation can’t assign. [rowspan] fails with NG8002, because the property is spelled rowSpan. The (click) line is fine: an event binding may assign. The last line is fine too. The build output includes:

// src/app/app.ts
import { Component } from '@angular/core';

@Component({
  selector: 'app-root',
  template: `
    <p>{{ total += 1 }}</p>
    <td [rowspan]="2">A</td>
    <button (click)="total = total + 1">Add</button>
    <p [attr.data-total]="total">{{ total > 3 ? 'many' : 'few' }}</p>
  `,
})
export class App {
  protected total = 0;
}
✘ [ERROR] NG8002: Can't bind to 'rowspan' since it isn't a known property of 'td'. Find more at https://next.angular.dev/errors/NG8002 [plugin angular-compiler]
...
✘ [ERROR] NG5002: Parser Error: Bindings cannot contain assignments at column 11 in [ total += 1 ] in

What’s next

Our examples change plain fields in event handlers, and that’s enough while every change starts with a click. In Part 3, we’ll see why a plain field sometimes doesn’t update the screen, and how signals and computed fix it for good.

How useful was this post?

Click on a heart to rate it!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate this post.