Template Statements
Angular Template Statements are used inside event bindings to execute actions when users interact with the application.
What are Template Statements?
Template statements are instructions written inside an Angular template that are executed in response to user actions such as button clicks, input events, mouse events, and keyboard events.
They are commonly used with Angular event binding to call methods or update component data.
Simple Definition
Angular Template Statements tell Angular what action should happen when a user interacts with the UI.
Template Statement Flow
Common Template Statements
Method Call
Calls a component method.
(click)="showMessage()"
Property Update
Updates component data.
(click)="count = count + 1"
Event Data
Uses data from an event.
(input)="name = $event.target.value"
Multiple Actions
Executes more than one simple action.
(click)="save(); reset()"
1. Template Statement with Event Binding
The most common use of a template statement is inside event binding. Angular executes the statement when the specified event occurs.
export class StudentComponent {
message = '';
showMessage() {
this.message = 'Welcome Pradnya!';
}
}
<button (click)="showMessage()">
Show Message
</button>
<p>
{{ message }}
</p>
Click the button and Welcome Pradnya! appears.
2. Updating a Property
A template statement can update a component property in response to an event.
export class StudentComponent {
count = 0;
}
<button (click)="count = count + 1">
Increase Count
</button>
<p>
Count: {{ count }}
</p>
Initially: Count: 0
After clicking the button: Count: 1
3. Using the Event Object
Template statements can pass the Angular event object to a
component method using $event.
<button (click)="handleClick($event)">
Click Me
</button>
handleClick(event: Event) {
console.log(event);
}
How it works
The $event object contains information about the
event that occurred in the browser.
4. Handling Input Events
Template statements can be used with input events to update component data as the user types.
export class StudentComponent {
studentName = '';
}
<input
(input)="studentName = $event.target.value"
placeholder="Enter student name">
<p>
Student Name: {{ studentName }}
</p>
If the user enters Sam, the UI displays:
Student Name: Sam
5. Calling a Method with Parameters
A template statement can call a component method and pass parameters to it.
export class StudentComponent {
message = '';
showStudent(name: string) {
this.message = 'Welcome ' + name;
}
}
<button (click)="showStudent('Sam')">
Show Student
</button>
<p>
{{ message }}
</p>
Welcome Sam
6. Executing Multiple Actions
A template statement can contain multiple simple actions separated by semicolons when required.
export class StudentComponent {
message = '';
count = 0;
save() {
this.message = 'Student Saved';
}
reset() {
this.count = 0;
}
}
<button (click)="save(); reset()">
Save Student
</button>
Best Practice
Multiple actions should remain simple. If the event handling becomes complicated, move the logic into a component method.
Template Expressions vs Template Statements
| Feature | Template Expression | Template Statement |
|---|---|---|
| Purpose | Evaluate a value | Perform an action |
| Common Usage | Interpolation / Binding | Event Binding |
| Example |
{{ marks + bonus }}
|
(click)="save()"
|
| Result | Produces a value | Performs an action |
Template Statement Examples
Method Call
(click)="save()"
Property Update
(click)="count = count + 1"
Event Object
(click)="handleClick($event)"
Method Parameter
(click)="showStudent('Sam')"
Complete Template Statement Example
export class StudentComponent {
studentName = 'Sam';
message = '';
count = 0;
showMessage() {
this.message = 'Welcome ' + this.studentName;
}
increaseCount() {
this.count++;
}
}
<h2>
Student Profile
</h2>
<p>
Name: {{ studentName }}
</p>
<button (click)="showMessage()">
Show Welcome Message
</button>
<p>
{{ message }}
</p>
<button (click)="increaseCount()">
Increase Count
</button>
<p>
Count: {{ count }}
</p>
Student Profile
Name: Sam
Welcome Sam
Count: 1
Template Statement Best Practices
- Use template statements mainly with event binding.
- Keep event statements short and readable.
- Use component methods for complex operations.
-
Use
$eventwhen event information is required. - Avoid putting large business logic directly inside templates.
-
Use meaningful method names such as
saveStudent()orshowMessage().
Summary
Angular Template Statements are used to execute actions in response to user events and interactions.
Common uses include Method Calls, Property Updates, Event Objects, Method Parameters and Multiple Simple Actions.
Template statements are mainly used with Event Binding and should remain simple and readable.