What is the first lifecycle hook called when an Angular component is created?
The first lifecycle hook called is ngOnChanges if the component has input properties. Otherwise, ngOnInit is called next.
Click to reveal answer
intermediate
Order these Angular lifecycle hooks in the sequence they are called during component creation: ngOnInit, ngOnChanges, ngDoCheck.
The order is: 1. ngOnChanges (called when input properties change) 2. ngOnInit (called once after first ngOnChanges) 3. ngDoCheck (called after ngOnChanges and ngOnInit for custom change detection)
Click to reveal answer
beginner
What lifecycle hook is called just before Angular destroys a component?
The ngOnDestroy hook is called just before Angular removes the component from the DOM. It is used to clean up resources like subscriptions.
Click to reveal answer
intermediate
Explain the purpose of ngAfterViewInit in Angular lifecycle.
ngAfterViewInit runs after Angular initializes the component's views and child views. It is useful to access or manipulate the DOM elements after they are rendered.
Click to reveal answer
advanced
Which lifecycle hooks run every time Angular runs change detection?
The hooks ngOnChanges (if inputs changed) and ngDoCheck run every change detection cycle. They help detect and respond to data changes.
Click to reveal answer
Which lifecycle hook is called only once after the first ngOnChanges?
AngDoCheck
BngOnInit
CngAfterViewInit
DngOnDestroy
✗ Incorrect
ngOnInit runs once after the first ngOnChanges to initialize the component.
When is ngOnDestroy called in Angular?
ABefore component destruction
BAfter every change detection
CAfter component initialization
DBefore ngOnInit
✗ Incorrect
ngOnDestroy is called just before Angular removes the component to clean up.
Which hook allows you to act after the component's child views are initialized?
AngOnDestroy
BngOnChanges
CngDoCheck
DngAfterViewInit
✗ Incorrect
ngAfterViewInit runs after the component's views and child views are fully initialized.
What triggers ngOnChanges to run?
AWhen input properties change
BAny change detection cycle
CWhen the component is destroyed
DAfter view initialization
✗ Incorrect
ngOnChanges runs only when input properties receive new values.
Which lifecycle hook is best for custom change detection logic?
AngOnInit
BngOnChanges
CngDoCheck
DngAfterViewInit
✗ Incorrect
ngDoCheck allows you to implement custom change detection beyond Angular's default.
Describe the order of Angular lifecycle hooks from component creation to destruction.
Think about what happens when a component starts, updates, and ends.
You got /5 concepts.
Explain when and why you would use ngOnDestroy in an Angular component.
Consider what happens when a component is removed from the page.
You got /4 concepts.
Practice
(1/5)
1. Which Angular lifecycle hook runs first when a component receives new input values? ngOnInit, ngOnChanges, ngAfterViewInit, or ngOnDestroy?
easy
A. ngOnChanges
B. ngOnInit
C. ngAfterViewInit
D. ngOnDestroy
Solution
Step 1: Understand lifecycle hooks on input change
When input properties change, Angular calls ngOnChanges first to react to those changes.
Step 2: Identify the first hook called on input update
ngOnInit runs only once after the first ngOnChanges, so it is not first.
Final Answer:
ngOnChanges -> Option A
Quick Check:
Input changes trigger ngOnChanges first [OK]
Hint: Remember: ngOnChanges always runs before ngOnInit on input change [OK]
Common Mistakes:
Thinking ngOnInit runs before ngOnChanges
Confusing ngAfterViewInit as first hook
Assuming ngOnDestroy runs on input change
2. Which of the following is the correct syntax to implement the ngOnDestroy lifecycle hook in an Angular component?
easy
A. destroy() { console.log('Destroyed'); }
B. onDestroy() { console.log('Destroyed'); }
C. ngOnDestroy() { console.log('Destroyed'); }
D. ngDestroy() { console.log('Destroyed'); }
Solution
Step 1: Recall Angular lifecycle hook naming
Angular lifecycle hooks have specific method names starting with 'ng', so ngOnDestroy is correct.
Step 2: Match method name exactly
Only ngOnDestroy() { console.log('Destroyed'); } uses the exact method name ngOnDestroy required by Angular.
Final Answer:
ngOnDestroy() { console.log('Destroyed'); } -> Option C
Quick Check:
Lifecycle hooks start with 'ngOn' [OK]
Hint: Lifecycle hooks always start with 'ngOn' followed by event [OK]
Common Mistakes:
Using incorrect method names like onDestroy or destroy
Missing 'ngOn' prefix
Confusing lifecycle hook names with custom methods