0
0
Angularframework~20 mins

ngAfterViewInit for view ready in Angular - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Angular View Lifecycle Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
lifecycle
intermediate
1:30remaining
When is ngAfterViewInit called in Angular?
In Angular, the ngAfterViewInit lifecycle hook is triggered at what point during a component's lifecycle?
AAfter the component is destroyed
BAfter the component's view and its child views have been fully initialized
CImmediately after the component's constructor runs
DBefore the component's inputs are set
Attempts:
2 left
💡 Hint
Think about when the component's template and child components are ready to interact with.
component_behavior
intermediate
1:30remaining
What happens if you access a child component's property in ngOnInit instead of ngAfterViewInit?
Consider a parent component that tries to access a child component's property inside ngOnInit. What is the likely outcome?
AThe parent component's ngOnInit is skipped
BThe child component's property is always fully initialized and accessible
CAngular throws a runtime error immediately
DThe child component's property may be undefined or not yet initialized
Attempts:
2 left
💡 Hint
Think about when child views are ready compared to ngOnInit timing.
📝 Syntax
advanced
2:00remaining
Identify the correct ngAfterViewInit implementation
Which of the following Angular component snippets correctly implements the ngAfterViewInit lifecycle hook?
Aexport class MyComponent implements AfterViewInit { ngAfterViewInit() { console.log('View ready'); } }
Bexport class MyComponent implements AfterViewInit { ngAfterViewInit { console.log('View ready'); } }
Cexport class MyComponent implements OnInit { ngAfterViewInit() { console.log('View ready'); } }
Dexport class MyComponent { ngAfterViewInit() { console.log('View ready'); } }
Attempts:
2 left
💡 Hint
Check interface implementation and method syntax.
🔧 Debug
advanced
2:00remaining
Why does accessing @ViewChild in ngOnInit cause an error?
Given this code snippet, why does accessing @ViewChild('myDiv') in ngOnInit cause an error?
 @ViewChild('myDiv') myDivElement: ElementRef;

ngOnInit() {
  console.log(this.myDivElement.nativeElement.textContent);
}
ABecause the view children are not yet initialized during ngOnInit
BBecause @ViewChild cannot be used with ElementRef
CBecause ngOnInit runs after ngAfterViewInit
DBecause nativeElement is not accessible in Angular
Attempts:
2 left
💡 Hint
Consider when Angular sets the references for view children.
state_output
expert
1:30remaining
What is the console output order for ngOnInit and ngAfterViewInit?
Given this Angular component code, what is the order of console logs when the component loads?
export class DemoComponent implements OnInit, AfterViewInit {
  ngOnInit() {
    console.log('Init');
  }
  ngAfterViewInit() {
    console.log('View Init');
  }
}
ABoth logs happen simultaneously
B"View Init" logged first, then "Init"
C"Init" logged first, then "View Init"
DNo logs appear because lifecycle hooks are not called
Attempts:
2 left
💡 Hint
Think about the sequence Angular calls lifecycle hooks.