Angular Templates
Property binding in Angular lets you set DOM element properties dynamically using values from your component class. This is a one-way flow: component ➝ template.
Two-way binding creates a live link between component data and template inputs. When the user types into a form field, the component property updates automatically, and any changes from the component are reflected back in the template instantly.
[property] to bind values from the component to DOM properties, e.g. [src], [disabled].(event) to listen for DOM events like (click), (input).[( )] with ngModel, e.g. [(ngModel)]="username", combining property + event binding.ngModel for two-way binding in template-driven forms.[propertyName]="componentProperty"[src]="imageUrl", [disabled]="buttonDisabled"[(ngModel)]="componentProperty"[(ngModel)]="username"[(ngModel)]="username" is roughly equal to [ngModel]="username" + (ngModelChange)="username = $event".
// app.component.ts - Property binding example
import { Component } from '@angular/core';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
imageUrl = 'https://angular.io/assets/images/logos/angular/angular.png';
buttonDisabled = true;
}
<!-- app.component.html - Template using property binding -->
<img [src]="imageUrl" alt="Angular Logo" width="150">
<br>
<button [disabled]="buttonDisabled">Click Me</button>
[src]="imageUrl" reads the imageUrl property from AppComponent and binds it to the src property of the <img> element.[disabled]="buttonDisabled" binds a boolean from the component to the button’s disabled property. If buttonDisabled is true, the button is disabled; if false, it becomes clickable.buttonDisabled in the component (e.g. via a method or event), Angular re-renders the view and updates the button state automatically.
// app.module.ts - Include FormsModule for ngModel
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule } from '@angular/forms';
import { AppComponent } from './app.component';
@NgModule({
declarations: [AppComponent],
imports: [BrowserModule, FormsModule],
bootstrap: [AppComponent]
})
export class AppModule {}
// app.component.ts - Property backing the input field
import { Component } from '@angular/core';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
username = '';
}
<!-- app.component.html - Two-way binding with ngModel -->
<input [(ngModel)]="username" placeholder="Enter your name">
<p>Hello, {{ username }}!</p>
[(ngModel)]="username" sends the current value of username from the component to the input field.ngModel emits an event and updates username in the component.Hello, {{ username }}! displays the latest value of username, updating live as the user types.[ ] when you only need one-way data flow from component to template (e.g. toggling disabled, setting image URLs, CSS classes, etc.).[(ngModel)] for form fields where you need tight synchronization between what the user types and the component state.FormsModule in your AppModule (or the relevant feature module) before using ngModel.disabled, value, checked) and not with plain HTML attributes.isVisible = true and use property binding with *ngIf to toggle the visibility of a paragraph: *ngIf="isVisible". Add a button to flip the value.value of an input box to a component property using [(ngModel)] and display it live below the input (like a preview).name and email, and show a live summary card that updates as the user types.isSubmitting and bind it to [disabled] on a submit button.