Full answer
Binding connects the class (data) to the template (screen). There are four forms.
- Interpolation,
{{ expression }}: prints the result of an expression as text. Example:{{ student }}. - Property binding,
[property]="expression": sets a property of a DOM element or an input of a component. Example:[disabled]="marks >= 100". The value is any expression, not only a string. - Event binding,
(event)="statement": runs code when an event happens, such as(click)="addMarks()". The built-in$eventgives the event object. - Two-way binding,
[(property)]="field": the "banana in a box" syntax. It is short for[property]="field"plus(propertyChange)="field = $event". With forms it is written[(ngModel)]and needsFormsModule.
There are also special property-like forms. [attr.aria-label] sets an HTML attribute, [class.passed]="isPassed" toggles one CSS class, and [style.width.px]="width" sets one style.
Property binding sets properties, not attributes. For most elements the two look the same, but they differ for things like value on an input, and for ARIA attributes that have no matching property (use [attr.*] for them).
In current Angular, a component can offer its own two-way binding with model(). A parent then writes [(rating)]="value" on the child.
Rules that help in practice:
- Expressions should be simple and cheap, because Angular evaluates them on every check. Put heavy work in a
computedor a method that is called once. - Interpolation never inserts HTML. If a student name contains
<b>, the browser shows the characters, not bold text. This is a basic protection against script injection. - Expressions cannot contain assignments (except in event statements),
new, or;chains. Use the class for logic. - Use the safe navigation operator for values that may be missing:
{{ student?.name }}. With signals, readstudent()?.name. - In an event binding, the statement is a small piece of code, so call one method.
(input)="name.set($any($event.target).value)"works, but a typed handler method is easier to test.
Example for a marks page: <input [value]="marks()" (input)="update($event)" /> shows the value from the class and sends each keystroke back, which is exactly what [(ngModel)] writes in one short form.
import { Component } from '@angular/core';
import { FormsModule } from '@angular/forms';
@Component({
selector: 'app-binding-demo',
imports: [FormsModule],
template: `
<h2>{{ student }} has {{ marks }} marks</h2>
<button (click)="addMarks(5)" [disabled]="marks >= 100">Add 5</button>
<p [class.passed]="marks >= 35" [attr.aria-label]="'Marks of ' + student">
Status: {{ marks >= 35 ? 'Pass' : 'Fail' }}
</p>
<input [(ngModel)]="student" placeholder="Student name" />
`,
styles: `.passed { color: green; }`,
})
export class BindingDemoComponent {
student = 'Ravi';
marks = 90;
addMarks(extra: number): void {
this.marks = Math.min(100, this.marks + extra);
}
}Angular 20.3, strict template type-checking passes. Add the component to a page and type in the input to see the heading change.
Key points
- `{{ }}` interpolation, `[prop]` property, `(event)` event, `[(prop)]` two-way.
- Two-way binding is property binding plus event binding.
- Use `[attr.name]` for attributes with no DOM property, `[class.x]` and `[style.x]` for single class and style.
- `[(ngModel)]` needs `FormsModule` in the component `imports`.
Common mistakes
- Writing `disabled="isLocked"` and expecting a binding. Without brackets it is the plain string "isLocked", which is truthy.
- Importing nothing for `ngModel` and getting the error "Can't bind to 'ngModel' since it isn't a known property of 'input'".
- Calling a heavy function inside interpolation. It runs on every change detection pass.
Follow-ups
- What is the difference between a DOM property and an HTML attribute?
- How does `[(ngModel)]` expand into property and event binding?
- How can your own component support `[(value)]`?
Tips
- Show you know two-way binding is sugar over two one-way bindings. This is the most common follow-up.
- Write the four forms from memory on the whiteboard, with one tiny example each.
- Mention `[attr.aria-label]` to show you think about accessibility.
Evaluation rubric
- Writes all four syntaxes correctly.
- Explains the expansion of two-way binding.
- Knows the attribute vs property difference.