0
0
Angularframework~20 mins

Why directives are needed in Angular - Challenge Your Understanding

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!
🧠 Conceptual
intermediate
2:00remaining
Why use directives in Angular?

Which of the following best explains why Angular uses directives?

ADirectives allow you to create reusable components and manipulate the DOM easily.
BDirectives are used only to style elements with CSS classes.
CDirectives replace the need for services and dependency injection.
DDirectives are only for handling HTTP requests in Angular.
Attempts:
2 left
💡 Hint

Think about how Angular helps you add behavior to HTML elements.

component_behavior
intermediate
2:00remaining
What happens when you use a structural directive?

Consider the Angular structural directive *ngIf. What does it do to the DOM?

AIt disables the element but keeps it visible in the DOM.
BIt only changes the style of the element without affecting the DOM structure.
CIt adds or removes elements from the DOM based on a condition.
DIt duplicates the element multiple times without any condition.
Attempts:
2 left
💡 Hint

Think about how *ngIf controls whether something shows or not.

📝 Syntax
advanced
2:30remaining
Identify the correct syntax for a custom attribute directive

Which option shows the correct way to define a custom attribute directive in Angular?

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

@Directive({
  selector: '[appHighlight]'
})
export class HighlightDirective {
  constructor(private el: ElementRef) {
    this.el.nativeElement.style.backgroundColor = 'yellow';
  }
}
AUse @Pipe with selector '[appHighlight]' and modify element style in constructor.
BUse @Component with selector 'appHighlight' and modify element style in ngOnInit.
CUse @Injectable with selector '[appHighlight]' and modify element style in constructor.
DUse @Directive with selector '[appHighlight]' and modify element style in constructor.
Attempts:
2 left
💡 Hint

Remember attribute directives use @Directive and selectors with square brackets.

lifecycle
advanced
2:30remaining
When is the best time to access the DOM in a directive?

In an Angular directive, which lifecycle hook is best to safely access and manipulate the DOM elements?

AngAfterViewInit
BngOnDestroy
CngOnChanges
DngOnInit
Attempts:
2 left
💡 Hint

Think about when the view and child elements are fully initialized.

🔧 Debug
expert
3:00remaining
Why does this directive not change the element's color?

Given this directive code, why does the element's text color not change to red?

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

@Directive({
  selector: '[appRedText]'
})
export class RedTextDirective {
  constructor(private el: ElementRef) {
    el.nativeElement.style.color = 'red';
  }
}
AThe directive selector should be without square brackets: 'appRedText'.
BThe directive is not declared in any Angular module, so it is not applied.
CThe directive is missing the <code>export</code> keyword before the class.
DThe style property 'color' cannot be set directly on nativeElement.
Attempts:
2 left
💡 Hint

Check if the directive is properly registered in Angular.