Template Expressions
Angular Template Expressions are used to evaluate component data, perform simple calculations, access properties, and display dynamic values inside an Angular template.
What are Template Expressions?
A template expression is an expression written inside an Angular template that Angular evaluates and uses to display a value or provide a value for a binding.
Template expressions usually work with component properties, object properties, simple calculations, method calls, and conditional expressions.
Simple Definition
Angular Template Expressions allow the template to read and calculate values from the component and display them in the UI.
Template Expression Flow
Common Template Expressions
Property
Displays a property from the component.
{{ studentName }}
Arithmetic
Performs simple calculations.
{{ marks + bonus }}
Method Call
Calls a component method.
{{ getStatus() }}
Conditional
Produces a value based on a condition.
marks >= 40 ? 'Pass' : 'Fail'
1. Property Expression
A property expression reads a value from the component class and displays it inside the Angular template.
export class StudentComponent {
studentName = 'Sam';
}
<h2>
Student Name: {{ studentName }}
</h2>
Student Name: Sam
2. Arithmetic Expressions
Angular template expressions can perform simple arithmetic calculations such as addition, subtraction, multiplication, and division.
export class StudentComponent {
marks = 80;
bonus = 5;
}
<p>
Total Marks:
{{ marks + bonus }}
</p>
Total Marks: 85
3. Method Call Expression
A template expression can call a component method and display the value returned by that method.
export class StudentComponent {
studentName = 'Sam';
getStudentLabel() {
return 'Student: ' + this.studentName;
}
}
<p>
{{ getStudentLabel() }}
</p>
Student: Sam
4. Object Property Expression
Template expressions can access properties inside an object defined in the component.
export class StudentComponent {
student = {
name: 'Sam',
course: 'Angular',
marks: 90
};
}
<p>
Name: {{ student.name }}
</p>
<p>
Course: {{ student.course }}
</p>
<p>
Marks: {{ student.marks }}
</p>
Name: Sam
Course: Angular
Marks: 90
5. Conditional Expression
A conditional expression can be used to choose between two values depending on whether a condition is true or false.
export class StudentComponent {
marks = 85;
}
<p>
Result:
{{ marks >= 40 ? 'Pass' : 'Fail' }}
</p>
Result: Pass
6. Logical Expressions
Logical operators can be used inside simple template expressions when multiple conditions need to be evaluated.
export class StudentComponent {
marks = 85;
attendance = 90;
}
<p>
Eligible:
{{ marks >= 40 && attendance >= 75 ? 'Yes' : 'No' }}
</p>
Eligible: Yes
7. Keeping Expressions Simple
Template expressions should be simple and focused on displaying or calculating values required by the user interface.
Good Example
<p>
Total:
{{ marks + bonus }}
</p>
Better Approach for Complex Logic
getStudentStatus() {
// Complex logic
return 'Pass';
}
Important
Avoid putting large business logic or complicated calculations directly inside the template. Keep complex logic inside the component or appropriate service.
Template Expression Examples
| Expression | Example | Purpose |
|---|---|---|
| Property |
{{ studentName }}
|
Display component data |
| Arithmetic |
{{ marks + bonus }}
|
Calculate values |
| Object Property |
{{ student.name }}
|
Access object data |
| Method Call |
{{ getStatus() }}
|
Get calculated value |
| Conditional |
marks >= 40 ? 'Pass' : 'Fail'
|
Display conditional value |
| Logical |
marks >= 40 && attendance >= 75
|
Evaluate multiple conditions |
Complete Template Expression Example
export class StudentComponent {
studentName = 'Sam';
marks = 85;
bonus = 5;
student = {
course: 'Angular Full Stack',
city: 'Pune'
};
getStatus() {
return this.marks >= 40 ? 'Pass' : 'Fail';
}
}
<div>
<h2>
Student Profile
</h2>
<p>
Name: {{ studentName }}
</p>
<p>
Course: {{ student.course }}
</p>
<p>
City: {{ student.city }}
</p>
<p>
Marks: {{ marks }}
</p>
<p>
Total Marks:
{{ marks + bonus }}
</p>
<p>
Result:
{{ marks >= 40 ? 'Pass' : 'Fail' }}
</p>
<p>
Status: {{ getStatus() }}
</p>
</div>
Student Profile
Name: Sam
Course: Angular Full Stack
City: Pune
Marks: 85
Total Marks: 90
Result: Pass
Status: Pass
Template Expression Best Practices
- Keep template expressions simple and readable.
- Use expressions mainly for UI-related values.
- Use component properties to display dynamic data.
- Move complex business logic into the component or service.
- Avoid unnecessary calculations inside templates.
- Use meaningful property and method names.
Summary
Angular Template Expressions allow templates to evaluate component data and display dynamic values in the user interface.
Common expressions include Property Expressions, Arithmetic Expressions, Object Property Expressions, Method Calls and Conditional Expressions.
Template expressions should remain simple, readable and focused on UI-related operations.