Angular Templates

What Are Angular Templates?

An Angular Template is the HTML code that defines the view of a component.

It is where you:

  • Display data

  • Handle user events

  • Use Angular directives

  • Bind expressions

Every component has a template.


📌 Where Templates Are Written?

1️⃣ External HTML file (most common)

Inside component folder:

hello.component.html


 

And the component references it:


 

2️⃣ Inline Template

You can also write template inside the component:


 


🧩 Angular Template Features

1️⃣ Interpolation (Showing Values)

Insert values using {{ }}:


 

TS code:

username = 'Sanjit';

2️⃣ Property Binding

Bind HTML properties:


 


3️⃣ Event Binding

Capture user events:



 


4️⃣ Two-Way Data Binding

Works with forms:



 


5️⃣ Structural Directives

These change the DOM structure:

*✔ ngIf


 

*✔ ngFor


 

*✔ ngSwitch


 


6️⃣ Attribute Directives

Modify element appearance:

✔ ngStyle


 

✔ ngClass


 


🧱 Template Expressions

These are small JS-like expressions inside {{ }}:


 

Not allowed:
❌ Assignments
❌ New objects
❌ Complex logic

Allowed:
✔ Method calls
✔ Property access
✔ Simple math


📘 Example: Full Angular Template

TS File


 

Template


 


🎯 Summary

Angular templates allow you to:

✔ Display data
✔ Bind UI and logic
✔ Use directives (*ngIf, *ngFor, etc.)
✔ Handle events
✔ Build dynamic UI

You may also like...