Performance: Inline vs external templates
This affects the initial page load speed and bundle size by determining how Angular loads and parses component templates.
import { Component } from '@angular/core'; @Component({ selector: 'app-example', templateUrl: './example.component.html' }) export class ExampleComponent {}
import { Component } from '@angular/core'; @Component({ selector: 'app-example', template: `<div><h1>Title</h1><p>Content here</p></div>` }) export class ExampleComponent {}
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Inline template | Same DOM nodes | Same reflows | Same paint cost | [X] Bad (larger JS bundle delays rendering) |
| External template | Same DOM nodes | Same reflows | Same paint cost | [OK] Good (smaller JS bundle, faster initial load) |
template property.templateUrl.template property with HTML inside the component file, while external templates use templateUrl to link an HTML file. -> Option Atemplate property with HTML string inside the component decorator.Hello World
' }) correctly usestemplate: 'Hello World
'. Others use wrong properties or external template syntax.@Component({
selector: 'app-sample',
template: `Title
{{ description }}
`
})
export class SampleComponent {
description = 'Inline template example';
}@Component({
selector: 'app-error',
templateUrl: './error.component.html'
})
export class ErrorComponent {
title = 'Error Example';
}error.component.html does not exist in the project folder.templateUrl requires the HTML file to exist at the specified path.@Component({
selector: 'app-message',
// Template choice here
})
export class MessageComponent {
message = 'Hello from Angular!';
}templatetemplateUrlPass the test and this pattern moves unknown