0
0
Angularframework~10 mins

Directive execution and DOM manipulation in Angular - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Directive execution and DOM manipulation
Angular Compiler Reads Template
Identify Directive on Element
Create Directive Instance
Call Directive Constructor
Run Directive Lifecycle Hooks
Directive Manipulates DOM or Host Element
Angular Updates Rendered DOM
User Interaction Triggers Directive Logic
Directive Updates DOM or State
Angular Reflects Changes in Browser
Angular reads the template, creates directive instances, runs their logic to manipulate the DOM, and updates the browser view accordingly.
Execution Sample
Angular
import { Directive, ElementRef } from '@angular/core';

@Directive({ selector: '[highlight]' })
export class HighlightDirective {
  constructor(el: ElementRef) {
    el.nativeElement.style.backgroundColor = 'yellow';
  }
}
This directive highlights the host element by setting its background color to yellow when created.
Execution Table
StepActionDirective StateDOM ChangeNotes
1Angular reads template and finds element with [highlight]No instanceNo changeDirective identified on element
2Angular creates HighlightDirective instanceInstance createdNo change yetConstructor about to run
3HighlightDirective constructor runsInstance activeSets backgroundColor to yellowDOM style updated
4Angular renders updated DOMInstance activeElement background is yellowUser sees highlight
5User clicks element (event triggers directive logic if any)Instance activeNo change (no event handler here)No further DOM update
6Directive lifecycle ends (component destroyed)Instance destroyedDOM cleaned if neededDirective removed
7End of directive lifecycleNo instanceDOM back to normal if cleanedExecution stops
💡 Directive lifecycle ends when component or element is destroyed, stopping DOM manipulation.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4Final
Directive InstanceNoneCreatedActiveActiveDestroyed
Element Style.backgroundColorundefinedundefined'yellow''yellow'undefined or reset
Key Moments - 3 Insights
Why does the DOM style change happen inside the constructor?
The constructor runs right after the directive instance is created (see step 3 in execution_table), so it can immediately manipulate the host element's DOM.
Does Angular update the DOM immediately after the directive changes it?
Yes, after the directive changes the DOM (step 3), Angular reflects those changes in the rendered DOM (step 4), so the user sees the update.
What happens if the directive has no event handlers?
If no event handlers exist (step 5), user interactions do not cause further DOM changes, so the DOM remains as last updated.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the DOM background color after step 3?
A'red'
Bundefined
C'yellow'
D'blue'
💡 Hint
Check the 'DOM Change' column at step 3 in the execution_table.
At which step does Angular render the updated DOM so the user sees the highlight?
AStep 2
BStep 4
CStep 3
DStep 5
💡 Hint
Look for when the 'Angular renders updated DOM' action happens in the execution_table.
If the directive had an event handler that changed the background to green on click, what would change in the execution_table?
AStep 5 would show a DOM change to green background
BStep 3 would change background to green
CStep 4 would be skipped
DStep 6 would happen earlier
💡 Hint
Consider the 'User clicks element' step and what happens if event logic changes DOM.
Concept Snapshot
Angular Directive Execution & DOM Manipulation:
- Angular finds directives in templates
- Creates directive instances and runs constructor
- Directive manipulates host element DOM (e.g., styles)
- Angular updates rendered DOM to show changes
- User events can trigger further DOM updates
- Directive lifecycle ends when element destroyed
Full Transcript
Angular directives are special classes that attach to elements in templates. When Angular compiles the template, it finds elements with directives and creates instances of those directives. The directive constructor runs immediately, allowing it to manipulate the host element's DOM, such as changing styles. Angular then updates the browser's rendered DOM so the user sees these changes. If the directive has event handlers, user interactions can trigger more DOM updates. Finally, when the component or element is destroyed, the directive instance is removed and any DOM cleanup happens. This flow ensures directives can control and update the DOM dynamically during the app's lifecycle.