1. Inline vs External Templates in Angular
What is a Template?
An Angular template defines the HTML UI displayed by a component.
Angular supports two main ways of defining a component template:
- Inline template — HTML is written directly inside the
@Componentdecorator. - External template — HTML is stored in a separate
.htmlfile.
Inline Template
An inline template is defined using the template property.
Code Sample
import { Component } from '@angular/core';
@Component({
selector: 'app-product',
template: `
<h2>{{ productName }}</h2>
<p>Price: ₹{{ price }}</p>
<button (click)="buyProduct()">Buy</button>
`
})
export class Product {
productName = 'Laptop';
price = 50000;
buyProduct() {
alert('Product purchased');
}
}
Explanation
The HTML is directly inside:
template: `...`
Backticks allow the HTML to span multiple lines.
There is no separate product.html file.
product.ts
│
├── TypeScript logic
└── HTML template
External Template
An external template keeps the HTML in a separate .html file and references it using templateUrl.
product.ts
import { Component } from '@angular/core';
@Component({
selector: 'app-product',
templateUrl: './product.html'
})
export class Product {
productName = 'Laptop';
price = 50000;
buyProduct() {
alert('Product purchased');
}
}
product.html
<h2>{{ productName }}</h2>
<p>Price: ₹{{ price }}</p>
<button (click)="buyProduct()">
Buy
</button>
Explanation
The component tells Angular where its template is located:
templateUrl: './product.html'
Now the files are separated:
product/
│
├── product.ts → Component logic
└── product.html → Component UI
Why Do We Need Both?
Not every component has the same amount of HTML.
For a very small component, creating a separate HTML file may be unnecessary.
For example:
@Component({
selector: 'app-loading',
template: `<p>Loading...</p>`
})
For a larger component containing forms, tables, buttons, conditions, loops, etc., keeping HTML inside the TypeScript file would make the component difficult to read.
In that situation, an external template is cleaner.
Inline vs External Template
| Feature | Inline Template | External Template |
|---|---|---|
| Property | template |
templateUrl |
| HTML location | TypeScript file | Separate HTML file |
| Best for | Very small templates | Normal/large templates |
| Readability | Good for small HTML | Better for large HTML |
| Separation | Logic and HTML together | Logic and HTML separated |
| File count | Fewer files | More files |
| Maintainability | Can become difficult | Usually easier |
| Reusability of component | Supported | Supported |
| Angular features | Fully supported | Fully supported |
When Should You Use Inline Templates?
Inline templates are useful for very small components such as:
@Component({
selector: 'app-loading',
template: `<span>Loading...</span>`
})
export class Loading {
}
Typical examples include:
- Loading indicators
- Simple labels
- Small status components
- Very small reusable UI elements
When Should You Use External Templates?
External templates are generally better for normal application components.
For example:
product-list/
├── product-list.ts
├── product-list.html
└── product-list.css
Use external templates when the HTML contains:
- Forms
- Tables
- Multiple UI sections
- Conditional rendering
- Loops
- Complex layouts
- Significant Angular template syntax
Advantages of Inline Templates
- Fewer files.
- Convenient for tiny components.
- Component logic and simple UI are visible together.
- Useful for quick/simple components.
Disadvantages of Inline Templates
- TypeScript file can become large.
- HTML becomes harder to read when the template grows.
- Mixes UI markup with TypeScript code.
- Large templates are harder to maintain.
Advantages of External Templates
- Better separation of concerns.
- Cleaner TypeScript files.
- Easier to maintain large HTML templates.
- Better organization for larger applications.
- Easier for developers to work specifically on markup.
Disadvantages of External Templates
- Creates additional files.
- For extremely small components, a separate HTML file may feel unnecessary.
Key Points
Remember the property names:
// Inline
template: `<h1>Hello</h1>`
// External
templateUrl: './product.html'
For real-world components with meaningful markup, external templates are commonly preferred for readability and maintainability. Inline templates are convenient when the markup is genuinely small.
2. Inline vs External Styles in Angular
What are Component Styles?
Styles define the appearance of an Angular component.
Angular allows CSS to be defined:
- Inline inside
@Component - Externally in a separate stylesheet
Inline Styles
Inline styles are placed directly inside the component metadata using styles.
Code Sample
import { Component } from '@angular/core';
@Component({
selector: 'app-product',
template: `
<h2>Laptop</h2>
<button>Buy</button>
`,
styles: `
h2 {
font-size: 24px;
font-weight: bold;
}
button {
padding: 10px;
}
`
})
export class Product {
}
Explanation
The CSS is directly defined inside:
styles: `...`
Therefore, everything exists in one TypeScript file:
product.ts
│
├── Component class
├── Template
└── Styles
This works well for very small components.
External Styles
External styles are stored in a separate CSS file.
product.ts
import { Component } from '@angular/core';
@Component({
selector: 'app-product',
templateUrl: './product.html',
styleUrl: './product.css'
})
export class Product {
}
product.html
<h2>Laptop</h2>
<button>Buy</button>
product.css
h2 {
font-size: 24px;
font-weight: bold;
}
button {
padding: 10px;
}
Now the responsibilities are separated:
product/
│
├── product.ts → Logic
├── product.html → UI
└── product.css → Styling
Why Do We Need Both?
Just like templates, some components require only a few CSS rules while others require significant styling.
For a tiny component:
@Component({
selector: 'app-status',
template: `<span>Active</span>`,
styles: `span { font-weight: bold; }`
})
Inline styling is perfectly reasonable.
For a larger component with many CSS rules, external styling provides better organization.
Inline vs External Styles
| Feature | Inline Styles | External Styles |
|---|---|---|
| Property | styles |
styleUrl / styleUrls |
| CSS location | TypeScript file | Separate stylesheet |
| Best for | Small styling | Medium/large styling |
| File count | Fewer files | More files |
| Readability | Good for small CSS | Better for large CSS |
| Maintainability | Harder when CSS grows | Easier |
| Separation | CSS mixed with TS metadata | CSS separated |
| Component styling | Supported | Supported |
styleUrl vs styleUrls
You may encounter both.
Single stylesheet
@Component({
selector: 'app-product',
templateUrl: './product.html',
styleUrl: './product.css'
})
styleUrl represents a single stylesheet.
Multiple stylesheets
@Component({
selector: 'app-product',
templateUrl: './product.html',
styleUrls: [
'./product.css',
'./product-theme.css'
]
})
styleUrls can reference multiple stylesheets.
Inline Template + Inline Styles
Both can be placed inside the TypeScript file:
@Component({
selector: 'app-message',
template: `
<h2>Welcome</h2>
<p>Hello Angular</p>
`,
styles: `
h2 {
font-weight: bold;
}
p {
font-size: 16px;
}
`
})
export class Message {
}
This gives a single-file component-like structure:
message.ts
├── Metadata
├── HTML
├── CSS
└── TypeScript logic
This is useful when the component is sufficiently small.
External Template + External Styles
For normal or larger components:
@Component({
selector: 'app-product',
templateUrl: './product.html',
styleUrl: './product.css'
})
export class Product {
}
Structure:
product/
│
├── product.ts
├── product.html
└── product.css
This provides clearer separation.
Advantages of Inline Styles
- Fewer files.
- Convenient for small components.
- Styling is visible alongside the component.
- Useful for simple UI elements.
Disadvantages of Inline Styles
- TypeScript file becomes larger.
- Large CSS becomes difficult to maintain.
- Less separation between styling and component code.
- Can reduce readability when the component grows.
Advantages of External Styles
- Better organization.
- Cleaner TypeScript code.
- Easier to maintain large stylesheets.
- Better separation of concerns.
- CSS tooling/editing is clearer in dedicated files.
Disadvantages of External Styles
- Additional files need to be maintained.
- Separate stylesheets may be unnecessary for components with only one or two simple rules.
Template and Style Comparison Together
The easiest way to remember all four properties is:
| Requirement | Inline | External |
|---|---|---|
| HTML | template |
templateUrl |
| CSS | styles |
styleUrl / styleUrls |
Example:
@Component({
// External HTML
templateUrl: './product.html',
// External CSS
styleUrl: './product.css'
})
versus:
@Component({
// Inline HTML
template: `<h2>Product</h2>`,
// Inline CSS
styles: `h2 { font-weight: bold; }`
})
Key Points
template→ inline HTML.templateUrl→ external HTML file.styles→ inline styles.styleUrl→ one external stylesheet.styleUrls→ multiple external stylesheets.- Inline templates/styles are convenient for small components.
- External templates/styles provide better separation for larger components.
- Both approaches support the same fundamental Angular component capabilities.
- Choosing inline vs external is primarily a matter of size, readability, and maintainability.
Interview Questions & Answers
Q1. What is the difference between template and templateUrl?
template defines HTML directly inside the component metadata, while templateUrl references an external HTML file.
template: `<h1>Hello</h1>`
vs.
templateUrl: './product.html'
Q2. What is the difference between styles and styleUrl?
styles defines CSS directly inside the component metadata, while styleUrl references an external stylesheet.
styles: `h1 { font-weight: bold; }`
vs.
styleUrl: './product.css'
Q3. When should you use an inline template?
Use it when the component has a very small and simple template where keeping the markup with the component improves readability.
Q4. When should you use an external template?
Use an external template when the HTML is larger or more complex. It keeps the TypeScript class cleaner and makes the UI easier to maintain.
Q5. Can a component use an external template and inline styles?
Yes. The approaches can be mixed:
@Component({
selector: 'app-product',
templateUrl: './product.html',
styles: `h2 { font-weight: bold; }`
})
Q6. Can a component use an inline template and external CSS?
Yes:
@Component({
selector: 'app-product',
template: `<h2>Product</h2>`,
styleUrl: './product.css'
})
Q7. Which approach is better?
Neither is universally better. Inline is convenient for very small components, while external files are generally easier to maintain when templates or styles become substantial.