Angular
Template Syntax & Binding
Interpolation, property, event, and two-way binding in Angular templates.
By EZ4Code Team
templatebinding
Code
import { Component } from "@angular/core";
import { FormsModule } from "@angular/forms";
@Component({
selector: "app-binding",
standalone: true,
imports: [FormsModule],
template: `
<p>Interpolation: {{ title }}</p>
<img [src]="imgUrl" [alt]="title" />
<button (click)="onClick()">Click</button>
<input [(ngModel)]="name" />
<p>Hi, {{ name }}</p>
`
})
export class BindingComponent {
title = "Template Syntax";
imgUrl = "/logo.png";
name = "";
onClick() { console.log("clicked"); }
}Explanation
Demonstrates Angular's four binding forms: interpolation renders values, [prop] binds properties, (event) binds events, and [(ngModel)] enables two-way binding. The template syntax compiles to efficient change-detection instructions at build time. Importing FormsModule is required for ngModel.
More Angular Snippets
Component Basics
Define an Angular component with selector, template, and styles.
Built-in Directives
Use *ngIf, *ngFor, ngClass, and ngStyle to shape the DOM.
Pipes & Custom Pipe
Transform template values with built-in and custom pipes.
Services & Dependency Injection
Create an injectable service and consume it in a component.
Routing Configuration
Define routes with params, lazy loading, and redirects.
Reactive Forms
Build a typed reactive form with FormBuilder and validators.