0
0
Angularframework~5 mins

Directive execution and DOM manipulation in Angular - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is an Angular directive?
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
Which lifecycle hook is ideal for DOM manipulation in a directive?
AngAfterViewInit
BngOnInit
CngOnDestroy
DngDoCheck
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
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
Which of these is NOT a type of Angular directive?
AEvent directive
BStructural directive
CAttribute directive
DComponent directive
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.