Bird
0
0

You want to focus an input element immediately after the view loads. Which is the best way to do this using ngAfterViewInit?

hard📝 component behavior Q8 of 15
Angular - Lifecycle Hooks
You want to focus an input element immediately after the view loads. Which is the best way to do this using ngAfterViewInit?
ACall focus() on the input element inside ngOnInit
BUse @ViewChild to get the input element and call its focus() method inside ngAfterViewInit
CUse setTimeout in ngOnInit to delay focus() call
DBind (click) event to focus input after view loads
Step-by-Step Solution
Solution:
  1. Step 1: Understand when view elements are ready

    Input elements are available after the view initializes, so ngAfterViewInit is the right place.
  2. Step 2: Use @ViewChild to access input and call focus()

    This ensures the input element exists and can be focused immediately.
  3. Final Answer:

    Use @ViewChild to get the input element and call its focus() method inside ngAfterViewInit -> Option B
  4. Quick Check:

    Focus input in ngAfterViewInit using @ViewChild [OK]
Quick Trick: Focus inputs safely in ngAfterViewInit with @ViewChild [OK]
Common Mistakes:
  • Trying to focus input in ngOnInit before view ready
  • Using setTimeout unnecessarily
  • Relying on user clicks to focus input

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Angular Quizzes