An Angular directive is a special marker in the DOM that tells Angular to attach behavior or modify the element. It helps add functionality to HTML elements.
Click to reveal answer
beginner
How does Angular execute a directive?
Angular runs directives during the component rendering process. It looks for directive selectors in the DOM and runs their logic to update or manipulate elements.
Click to reveal answer
intermediate
Which Angular lifecycle hook is best for DOM manipulation inside a directive?
The ngAfterViewInit lifecycle hook is best for DOM manipulation because it runs after Angular has fully initialized the component's view.
Click to reveal answer
intermediate
Why should direct DOM manipulation be done carefully in Angular directives?
Direct DOM manipulation can cause issues with Angular's rendering and change detection. It's best to use Angular's Renderer2 service to safely manipulate the DOM.
Click to reveal answer
intermediate
What is Renderer2 in Angular and why use it in directives?
Renderer2 is a service that provides safe methods to manipulate the DOM. It helps keep your app compatible with different platforms and avoids direct DOM access.
Click to reveal answer
When does Angular execute a directive's logic?
AOnly when the page reloads
BDuring component rendering when the directive selector matches
CBefore the component is created
DOnly after the user clicks a button
✗ Incorrect
Angular runs directive logic during component rendering when it finds matching selectors in the DOM.
Which lifecycle hook is ideal for DOM manipulation in a directive?
AngAfterViewInit
BngOnInit
CngOnDestroy
DngDoCheck
✗ Incorrect
ngAfterViewInit runs after the component's view has been fully initialized, making it safe for DOM manipulation.
Why use Renderer2 instead of direct DOM access in Angular directives?
AIt only works in browsers
BIt makes the app slower
CIt disables change detection
DIt ensures safe and platform-independent DOM manipulation
✗ Incorrect
Renderer2 provides safe methods to manipulate the DOM and keeps the app compatible across platforms.
What happens if you manipulate the DOM directly in Angular without Renderer2?
AAngular automatically fixes it
BIt improves performance
CIt can cause rendering and change detection issues
DIt disables directives
✗ Incorrect
Direct DOM manipulation can interfere with Angular's rendering and change detection, causing bugs.
Which of these is NOT a type of Angular directive?
AEvent directive
BStructural directive
CAttribute directive
DComponent directive
✗ Incorrect
Angular has component, structural, and attribute directives. 'Event directive' is not a type.
Explain how Angular executes directives and how you can safely manipulate the DOM inside a directive.
Think about Angular's rendering process and lifecycle hooks.
You got /4 concepts.
Describe why direct DOM manipulation can be problematic in Angular and how Renderer2 helps.
Consider Angular's rendering and platform support.
You got /4 concepts.
Practice
(1/5)
1. What is the primary purpose of an Angular directive?
easy
A. To create new Angular modules
B. To add or change behavior on HTML elements
C. To define routes in the application
D. To manage HTTP requests
Solution
Step 1: Understand what directives do
Directives in Angular are used to add or change behavior on HTML elements, such as changing styles or responding to events.
Step 2: Compare with other Angular features
Modules organize code, routes manage navigation, and HTTP requests handle data fetching, which are not the main roles of directives.
Final Answer:
To add or change behavior on HTML elements -> Option B
Quick Check:
Directive purpose = Add/change behavior [OK]
Hint: Directives change element behavior, not app structure [OK]
Common Mistakes:
Confusing directives with modules
Thinking directives manage routing
Assuming directives handle HTTP
2. Which syntax correctly injects Renderer2 in a directive's constructor?
Angular uses TypeScript constructor parameters with access modifiers like 'private' before the variable name and type after the colon.
Step 2: Match correct syntax
constructor(private renderer: Renderer2) {} follows the pattern: 'constructor(private renderer: Renderer2) {}'. Other options have incorrect order or missing colons.
Final Answer:
constructor(private renderer: Renderer2) {} -> Option D
Quick Check:
Correct DI syntax = private name: Type [OK]
Hint: Use 'private name: Type' in constructor for DI [OK]
Common Mistakes:
Swapping parameter order
Omitting colon between name and type
Placing access modifier after type
3. Given this directive code, what will happen when the user clicks the host element?
A. Missing access modifiers in constructor parameters prevent DI
B. Renderer2 cannot set styles directly
C. Selector syntax is invalid
D. ElementRef should not be used in directives
Solution
Step 1: Check constructor parameters for DI
Angular dependency injection requires constructor parameters to have access modifiers like 'private' or 'public' to assign them as class properties.
Step 2: Identify missing access modifiers
Here, 'el' and 'renderer' lack 'private' or 'public', so Angular won't inject them properly, causing runtime errors.
Final Answer:
Missing access modifiers in constructor parameters prevent DI -> Option A
Quick Check:
DI needs access modifiers in constructor [OK]
Hint: Always add 'private' or 'public' for DI in constructor [OK]
Common Mistakes:
Thinking Renderer2 can't set styles
Believing selector syntax is wrong
Assuming ElementRef is disallowed
5. You want to create a directive that toggles a CSS class active on the host element when the user hovers over it, and removes it when the mouse leaves. Which approach correctly implements this behavior?
hard
A. Use two @HostListener methods for 'mouseenter' and 'mouseleave' to add and remove the class using Renderer2
B. Use a single @HostListener for 'click' to toggle the class
C. Directly modify the element's classList without Renderer2 inside the constructor
D. Use @HostBinding to bind a boolean and toggle it manually without event listeners
Solution
Step 1: Understand event requirements
Hover behavior requires reacting to 'mouseenter' and 'mouseleave' events to add and remove the class.
Step 2: Use Renderer2 with @HostListener
Using two @HostListener methods for these events and Renderer2 to add/remove the 'active' class is the safe and correct Angular way.
Final Answer:
Use two @HostListener methods for 'mouseenter' and 'mouseleave' to add and remove the class using Renderer2 -> Option A