0
0
Angularframework~20 mins

Directive execution and DOM manipulation in Angular - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Directive Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What will be the rendered output of this Angular directive?

Consider this Angular standalone directive that changes the background color of the host element:

import { Directive, ElementRef, Renderer2 } from '@angular/core';

@Directive({
  selector: '[appHighlight]',
  standalone: true
})
export class HighlightDirective {
  constructor(private el: ElementRef, private renderer: Renderer2) {
    this.renderer.setStyle(this.el.nativeElement, 'background-color', 'yellow');
  }
}

If this directive is applied to a <div> with text 'Hello', what will the user see?

Angular
<div appHighlight>Hello</div>
AThe text color will change to yellow but background remains default
BThe text 'Hello' will be bold but no background color change
CThe div will be hidden and not visible on the page
DA yellow background behind the text 'Hello' inside the div
Attempts:
2 left
💡 Hint

Think about what renderer.setStyle does to the host element's style.

lifecycle
intermediate
1:30remaining
When is the best lifecycle hook to safely manipulate the DOM in an Angular directive?

You want to add a CSS class to the host element after it is fully initialized. Which lifecycle hook should you use inside your directive?

Aconstructor
BngOnInit
CngAfterViewInit
DngOnDestroy
Attempts:
2 left
💡 Hint

Consider when the view and child elements are fully rendered.

📝 Syntax
advanced
2:30remaining
Which directive code snippet correctly toggles a CSS class on click?

Choose the correct Angular directive code that toggles the CSS class 'active' on the host element when it is clicked.

A
import { Directive, HostListener, Renderer2, ElementRef } from '@angular/core';

@Directive({ selector: '[appToggle]' })
export class ToggleDirective {
  private active = false;
  constructor(private el: ElementRef, private renderer: Renderer2) {}
  @HostListener('click') toggle() {
    this.active = !this.active;
    if (this.active) {
      this.renderer.addClass(this.el.nativeElement, 'active');
    } else {
      this.renderer.removeClass(this.el.nativeElement, 'active');
    }
  }
}
B
import { Directive, HostListener, Renderer2, ElementRef } from '@angular/core';

@Directive({ selector: '[appToggle]' })
export class ToggleDirective {
  private active = false;
  constructor(private el: ElementRef, private renderer: Renderer2) {}
  @HostListener('click') toggle() {
    this.active = !this.active;
    this.el.nativeElement.classList.toggle('active');
  }
}
C
import { Directive, HostListener, Renderer2, ElementRef } from '@angular/core';

@Directive({ selector: '[appToggle]' })
export class ToggleDirective {
  private active = false;
  constructor(private el: ElementRef, private renderer: Renderer2) {}
  @HostListener('click') toggle() {
    this.active = !this.active;
    this.renderer.setStyle(this.el.nativeElement, 'class', this.active ? 'active' : '');
  }
}
D
import { Directive, HostListener, Renderer2, ElementRef } from '@angular/core';

@Directive({ selector: '[appToggle]' })
export class ToggleDirective {
  private active = false;
  constructor(private el: ElementRef, private renderer: Renderer2) {}
  @HostListener('click') toggle() {
    this.active = !this.active;
    this.renderer.setAttribute(this.el.nativeElement, 'class', this.active ? 'active' : '');
  }
}
Attempts:
2 left
💡 Hint

Remember that renderer.setStyle does not set classes, and direct DOM manipulation is discouraged.

🔧 Debug
advanced
2:00remaining
What error will this Angular directive code cause?

Analyze this directive code snippet:

import { Directive, ElementRef, Renderer2, HostListener } from '@angular/core';

@Directive({ selector: '[appError]' })
export class ErrorDirective {
  constructor(private el: ElementRef, private renderer: Renderer2) {
    this.renderer.setStyle(this.el.nativeElement, 'background-color', 'blue');
  }

  @HostListener('click') onClick() {
    this.renderer.setStyle(this.el.nativeElement, 'color', 'white');
  }
}

What error will Angular throw when this directive is used?

ASyntaxError due to missing semicolons
BNo error; the directive works as expected
CRuntime error: HostListener decorator missing import
DRuntime error: Cannot read property 'setStyle' of undefined
Attempts:
2 left
💡 Hint

Check if all necessary imports and syntax are correct.

🧠 Conceptual
expert
3:00remaining
Which statement best describes Angular directive execution order and DOM manipulation?

Consider multiple directives applied to the same element. Which statement about their execution and DOM manipulation order is true?

AAll directives execute their constructors first, then lifecycle hooks run in the order of declaration, allowing safe DOM manipulation in ngAfterViewInit
BDirectives execute in the order they appear in the template, and each can manipulate the DOM immediately during construction
CDirectives execute lifecycle hooks in parallel, so DOM manipulation order is unpredictable
DDirectives cannot manipulate the DOM; only components can
Attempts:
2 left
💡 Hint

Think about Angular's lifecycle and when DOM is ready for manipulation.