Directives in Angular help change how the page looks or behaves. Structural directives change the page layout by adding or removing elements. Attribute directives change how elements look or behave without changing the layout.
Structural vs attribute directives in Angular
Structural directive syntax: *directiveName="expression" Attribute directive syntax: [directiveName]="expression" or directiveName without brackets
Structural directives use an asterisk (*) before their name.
Attribute directives are used as normal HTML attributes, sometimes with square brackets.
isVisible.<div *ngIf="isVisible">Show this if true</div>
li for each item in the items list.<li *ngFor="let item of items">{{ item }}</li>isDisabled is true.<button [disabled]="isDisabled">Click me</button><p appHighlight>Highlighted text</p>
This Angular example shows both structural and attribute directives:
*ngIfshows or hides the list.*ngForrepeats list items.appHighlightis a custom attribute directive that changes the background color of each list item and changes color on mouse hover.
Clicking the button toggles the list visibility.
import { Component, Directive, ElementRef, HostListener, Renderer2 } from '@angular/core'; @Directive({ selector: '[appHighlight]' }) export class HighlightDirective { constructor(private el: ElementRef, private renderer: Renderer2) { this.renderer.setStyle(this.el.nativeElement, 'backgroundColor', 'yellow'); } @HostListener('mouseenter') onMouseEnter() { this.renderer.setStyle(this.el.nativeElement, 'backgroundColor', 'orange'); } @HostListener('mouseleave') onMouseLeave() { this.renderer.setStyle(this.el.nativeElement, 'backgroundColor', 'yellow'); } } @Component({ selector: 'app-root', template: ` <button (click)="toggle()">Toggle List</button> <ul *ngIf="showList"> <li *ngFor="let item of items" appHighlight>{{ item }}</li> </ul> ` }) export class AppComponent { showList = true; items = ['Apple', 'Banana', 'Cherry']; toggle() { this.showList = !this.showList; } }
Structural directives change the HTML layout by adding or removing elements.
Attribute directives change the look or behavior of existing elements.
Use * only with structural directives.
Structural directives add or remove elements from the page.
Attribute directives change how elements look or behave without changing layout.
Examples: *ngIf and *ngFor are structural; [disabled] and custom directives like appHighlight are attribute directives.
Practice
Solution
Step 1: Understand what structural directives do
Structural directives modify the DOM layout by adding or removing elements dynamically.Step 2: Compare with other directive types
Attribute directives change appearance or behavior but do not add or remove elements.Final Answer:
It changes the layout by adding or removing elements from the DOM. -> Option CQuick Check:
Structural directive = changes DOM structure [OK]
- Confusing attribute directives with structural directives
- Thinking attribute directives add or remove elements
- Mixing event listeners with directive types
Solution
Step 1: Recall the syntax for the structural directive *ngFor
The correct syntax uses an asterisk (*) before ngFor and a let variable to iterate.Step 2: Check each option's syntax
Only<div *ngFor="let item of items">{{item}}</div>uses*ngFor="let item of items"which is correct.Final Answer:
<div *ngFor="let item of items">{{item}}</div> -> Option AQuick Check:
*ngFor syntax =<div *ngFor="let item of items">{{item}}</div>[OK]
- Using square brackets [] instead of * for structural directives
- Using parentheses () which are for events
- Using template reference (#) incorrectly
<ul> <li *ngIf="showItem">Item 1</li> <li>Item 2</li> </ul>
Assuming
showItem is false.Solution
Step 1: Understand *ngIf behavior with false condition
*ngIf removes the element from the DOM if the condition is false.Step 2: Apply this to the template
The first <li> with *ngIf="showItem" will not render because showItem is false. The second <li> renders normally.Final Answer:
<ul><li>Item 2</li></ul> -> Option DQuick Check:
*ngIf false removes element [OK]
- Thinking *ngIf hides element but keeps it in DOM
- Assuming both list items render regardless
- Confusing *ngIf with attribute directives
<button *ngIf="isEnabled" [disabled]="isDisabled">Click Me</button>
Assuming
isEnabled and isDisabled are boolean variables.Solution
Step 1: Check compatibility of *ngIf and [disabled]
*ngIf controls element presence; [disabled] controls attribute binding. Both can coexist.Step 2: Verify syntax correctness
The syntax uses *ngIf with a boolean and [disabled] with a boolean, which is valid Angular syntax.Final Answer:
The syntax is correct; no error present. -> Option AQuick Check:
*ngIf and [disabled] can be used together [OK]
- Thinking *ngIf and attribute bindings cannot be combined
- Confusing *ngIf with [ngIf]
- Replacing attribute bindings with structural directive syntax
Solution
Step 1: Understand the directive's purpose
The directive changes background color on hover, affecting appearance only.Step 2: Choose directive type based on behavior
Attribute directives change element style or behavior without adding/removing elements, fitting this case.Final Answer:
Attribute directive, because it changes the element's appearance without altering layout. -> Option BQuick Check:
Appearance change = attribute directive [OK]
- Choosing structural directive for style changes
- Confusing event listening with directive type
- Thinking attribute directives add/remove elements
Ready to prove it?
Pass the test and this pattern moves unknown
