Template Syntax
Angular Template Syntax provides special syntax that allows HTML templates to display component data, bind properties, handle events, and create interactive user interfaces.
What is Template Syntax?
Template syntax is the collection of Angular features that can be used inside an HTML template.
It connects the component class with the user interface and allows the UI to react to application data and user actions.
Simple Definition
Angular Template Syntax makes normal HTML dynamic and interactive.
Template Syntax Flow
Main Angular Template Syntax
Interpolation
Displays component data in the template.
{{ name }}
Property Binding
Binds a component value to an element property.
[value]="name"
Event Binding
Responds to user events.
(click)="save()"
Two-Way Binding
Synchronizes data between input and component.
[(ngModel)]="name"
1. Interpolation
Interpolation uses double curly braces to display values from the component class.
export class StudentComponent {
studentName = 'Sam';
}
<h2>Student Name: {{ studentName }}</h2>
Student Name: Sam
2. Property Binding
Property binding connects a component property to a property of an HTML element or Angular component.
export class StudentComponent {
imageUrl = 'assets/student.jpg';
}
<img [src]="imageUrl" alt="Student">
3. Event Binding
Event binding allows a template to respond to user actions such as button clicks.
export class StudentComponent {
message = '';
showMessage() {
this.message = 'Welcome Pradnya!';
}
}
<button (click)="showMessage()">
Show Message
</button>
<p>{{ message }}</p>
After clicking the button, the message Welcome Pradnya! appears.
4. Two-Way Binding
Two-way binding allows data to move between the component and the input element.
export class StudentComponent {
studentName = '';
}
<input
[(ngModel)]="studentName"
placeholder="Enter student name">
<p>Student Name: {{ studentName }}</p>
5. Attribute Binding
Attribute binding can be used when you need to bind an HTML attribute rather than a DOM property.
studentId = 101;
<div [attr.data-student-id]="studentId">
Student Information
</div>
6. Class Binding
Class binding allows CSS classes to be added or removed based on component data.
isActive = true;
<div [class.active]="isActive">
Student Status
</div>
7. Style Binding
Style binding allows individual CSS style properties to be controlled using component values.
fontSize = 20;
<p [style.font-size.px]="fontSize">
Angular Student
</p>
8. Template Reference Variable
A template reference variable gives a convenient reference to an element or directive instance within the template.
<input #studentInput placeholder="Enter name">
<button (click)="showName(studentInput.value)">
Show Name
</button>
How it works
#studentInput creates a reference to the input
element, allowing the template to access its current value.
9. Template Expressions
Angular template expressions can evaluate values and simple expressions.
marks = 85;
total = 100;
<p>
Percentage:
{{ (marks / total) * 100 }}%
</p>
10. Event Object
Event handlers can receive the browser event object when required.
<button (click)="handleClick($event)">
Click Me
</button>
handleClick(event: Event) {
console.log(event);
}
11. Control Flow in Templates
Modern Angular templates support control-flow syntax for conditional rendering and repeated content.
Conditional Rendering
@if (isLoggedIn) {
<p>Welcome Sam</p>
}
Repeated Content
@for (student of students; track student) {
<p>{{ student }}</p>
}
Template Syntax Quick Reference
| Syntax | Purpose | Example |
|---|---|---|
| Interpolation | Display component data | {{ name }} |
| Property Binding | Bind element property | [value]="name" |
| Event Binding | Handle user events | (click)="save()" |
| Two-Way Binding | Synchronize data | [(ngModel)]="name" |
| Attribute Binding | Bind HTML attributes | [attr.id]="studentId" |
| Class Binding | Control CSS classes | [class.active]="isActive" |
| Style Binding | Control CSS styles | [style.font-size.px]="size" |
| Reference Variable | Reference template element | #studentInput |
Complete Template Syntax Example
export class StudentComponent {
studentName = 'Sam';
course = 'Angular Full Stack';
isActive = true;
showMessage() {
alert('Welcome ' + this.studentName);
}
}
<div [class.active]="isActive">
<h2>Student Profile</h2>
<p>
Name: {{ studentName }}
</p>
<p>
Course: {{ course }}
</p>
<button (click)="showMessage()">
Welcome
</button>
</div>
Student Profile
Name: Sam
Course: Angular Full Stack
Template Syntax Best Practices
- Keep template expressions simple.
- Use property binding instead of manually changing DOM properties.
- Use event binding for user interactions.
- Avoid complex business logic inside templates.
- Use reusable components when templates become too large.
- Keep binding names meaningful and easy to understand.
Summary
Angular Template Syntax connects component data and logic with the HTML user interface.
Important syntax includes Interpolation, Property Binding, Event Binding, Two-Way Binding, Class Binding, Style Binding and Template Reference Variables.
Understanding template syntax is essential for creating dynamic and interactive Angular applications.