Angular Component Template
A component template defines the user interface of an Angular component. It contains HTML along with Angular template syntax used to display and interact with application data.
What is a Component Template?
A component template is an HTML file associated with an Angular component. It defines what should be displayed in the browser.
Angular extends normal HTML with features such as interpolation, property binding, event binding, directives and control flow.
Component Template Flow
Component Class
Stores data and application logic.
Template
Uses HTML and Angular syntax.
Angular
Processes the template.
Browser
Displays the final UI.
External Template
An external template is stored in a separate HTML file and connected to the component using templateUrl.
import { Component } from '@angular/core';
@Component({
selector: 'app-student',
templateUrl: './student.component.html'
})
export class StudentComponent {
studentName = 'Sam';
}
<h2>Student Details</h2>
<p>
Student Name: {{ studentName }}
</p>
Inline Template
Instead of using a separate HTML file, a component can define its template directly using the template property.
import { Component } from '@angular/core';
@Component({
selector: 'app-student',
template: `
<h2>Student Details</h2>
<p>Student Name: Sam</p>
`
})
export class StudentComponent {
}
When to Use?
Inline templates can be useful for small components. Larger templates are generally easier to maintain in separate HTML files.
Interpolation in Templates
Interpolation is used to display component data inside HTML. It uses double curly braces.
export class StudentComponent {
studentName = 'Sam';
}
<h2>Student Details</h2>
<p>
Student Name: {{ studentName }}
</p>
Expected Output
Student Name: Sam
Property Binding in Templates
Property binding connects a component property with a property of an HTML element.
export class StudentComponent {
imageUrl = 'assets/student.png';
}
<img [src]="imageUrl" alt="Student">
Event Binding in Templates
Event binding allows the template to respond to user actions such as button clicks.
export class StudentComponent {
message = '';
showMessage() {
this.message = 'Welcome Sam';
}
}
<button (click)="showMessage()">
Show Message
</button>
<p>
{{ message }}
</p>
Expected Output
After clicking the button: Welcome Sam
Template Expressions
Angular templates can use expressions to display values calculated from component data.
export class StudentComponent {
marks = 85;
}
<p>
Marks: {{ marks }}
</p>
<p>
Total: {{ marks + 15 }}
</p>
Expected Output
Marks: 85
Total: 100
Common Angular Template Features
Interpolation
{{ value }}
Displays component data.
Property Binding
[property]
Binds values to element properties.
Event Binding
(click)
Handles user events.
Template Features
@if / @for
Controls how template content is rendered.
Data Flow Between Component and Template
Template Best Practices
Keep templates simple and readable.
Keep complex business logic inside the component or services instead of the template.
Use meaningful property and method names.
Reuse components where appropriate.
Summary
An Angular component template defines the user interface of a component.
Templates can use HTML together with Angular features such as interpolation, property binding and event binding.
Templates connect component data and logic with the user interface displayed in the browser.