0
0
Angularframework~10 mins

Custom structural directives in Angular - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Custom structural directives
Start: Angular template
Detect *directive syntax
Call directive's constructor
Directive receives TemplateRef & ViewContainerRef
Directive decides when/how to add/remove views
ViewContainerRef creates embedded views
Angular renders embedded views in DOM
Directive updates views on input changes
End: DOM reflects directive logic
Angular reads the template, finds the custom structural directive, then uses its logic to add or remove parts of the DOM dynamically.
Execution Sample
Angular
import { Directive, Input, TemplateRef, ViewContainerRef } from '@angular/core';

@Directive({ selector: '[appShow]' })
export class ShowDirective {
  constructor(private tpl: TemplateRef<any>, private vcr: ViewContainerRef) {}

  @Input() set appShow(condition: boolean) {
    this.vcr.clear();
    if (condition) this.vcr.createEmbeddedView(this.tpl);
  }
}
This directive shows or hides a template part based on a boolean input.
Execution Table
StepActionInput ConditionViewContainerRef StateDOM Change
1Directive instantiatedN/AEmptyNo DOM added
2Input appShow set to truetrueCleared then view createdTemplate inserted in DOM
3Input appShow set to falsefalseClearedTemplate removed from DOM
4Input appShow set to true againtrueCleared then view createdTemplate re-inserted in DOM
5No further input changesN/AView remainsDOM unchanged
6End of traceN/AFinal state keptDOM reflects last input
💡 Execution stops as no more input changes occur; DOM matches directive logic.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4Final
conditionundefinedtruefalsetruetrue
ViewContainerRef stateemptyview createdclearedview createdview created
Key Moments - 3 Insights
Why does the directive clear the ViewContainerRef before creating a new view?
Clearing removes any previously rendered views to avoid duplicates. See execution_table rows 2 and 3 where clearing happens before creating or removing views.
What happens if the input condition is false?
The directive clears the container and does not create a view, so the template is removed from the DOM. See execution_table row 3.
How does Angular know where to insert the template in the DOM?
Angular uses the ViewContainerRef linked to the directive's host element to insert or remove embedded views dynamically.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the ViewContainerRef state after step 3?
AView created
BUnchanged
CCleared
DError state
💡 Hint
Check the 'ViewContainerRef State' column in row 3 of the execution_table.
At which step does the template get inserted back into the DOM after being removed?
AStep 2
BStep 4
CStep 3
DStep 5
💡 Hint
Look for when the input condition changes back to true and a view is created again.
If the input condition never changes from true, how would the execution_table change?
ANo clearing steps would occur
BViews would be created and cleared repeatedly
CThe template would never be inserted
DThe directive would throw an error
💡 Hint
Refer to variable_tracker for condition changes and execution_table for clearing actions.
Concept Snapshot
Custom structural directives in Angular
- Use @Directive with selector like '[appShow]'
- Inject TemplateRef and ViewContainerRef
- Control DOM by creating/removing embedded views
- Use input setters to react to changes
- Clear container before adding views to avoid duplicates
Full Transcript
Custom structural directives in Angular let you add or remove parts of the page dynamically. Angular reads the template and finds your directive marked with a star (*). It calls your directive's constructor, giving you the template and a place to insert views. Your directive decides when to add or remove these views based on input values. For example, if the input is true, it creates the view and inserts it into the DOM. If false, it clears the container, removing the view. This process repeats whenever the input changes, keeping the page in sync with your logic.