0
0
Angularframework~20 mins

@ViewChild decorator usage in Angular - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Angular @ViewChild Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What will be the output of this Angular component using @ViewChild?

Consider this Angular component code snippet:

import { Component, ViewChild, ElementRef, AfterViewInit } from '@angular/core';

@Component({
  selector: 'app-sample',
  template: `<input #inputRef type='text' value='Hello' />` 
})
export class SampleComponent implements AfterViewInit {
  @ViewChild('inputRef') inputElement!: ElementRef;

  ngAfterViewInit() {
    console.log(this.inputElement.nativeElement.value);
  }
}

What will be logged to the console when this component renders?

Angular
import { Component, ViewChild, ElementRef, AfterViewInit } from '@angular/core';

@Component({
  selector: 'app-sample',
  template: `<input #inputRef type='text' value='Hello' />` 
})
export class SampleComponent implements AfterViewInit {
  @ViewChild('inputRef') inputElement!: ElementRef<HTMLInputElement>;

  ngAfterViewInit() {
    console.log(this.inputElement.nativeElement.value);
  }
}
Aundefined
Bnull
C"Hello"
DError: inputElement is undefined
Attempts:
2 left
💡 Hint

Remember when @ViewChild references are available in the component lifecycle.

📝 Syntax
intermediate
2:00remaining
Which @ViewChild syntax correctly queries a component by its class type?

Given a child component named ChildComponent, which @ViewChild declaration correctly gets its instance?

Angular
import { Component, ViewChild } from '@angular/core';
import { ChildComponent } from './child.component';

@Component({
  selector: 'app-parent',
  template: `<child-component></child-component>`
})
export class ParentComponent {
  // Which @ViewChild is correct?
}
A@ViewChild('ChildComponent') child!: ChildComponent;
B@ViewChild(ChildComponent) child!: ChildComponent;
C@ViewChild() child!: ChildComponent;
D@ViewChild('child') child!: ChildComponent;
Attempts:
2 left
💡 Hint

When querying by component class, you pass the class itself, not a string.

lifecycle
advanced
2:00remaining
When is the @ViewChild reference available in the component lifecycle?

In which lifecycle hook is the @ViewChild property guaranteed to be initialized and safe to use?

AngAfterViewInit()
BngOnInit()
Cconstructor()
DngAfterContentInit()
Attempts:
2 left
💡 Hint

Think about when the component's view is fully rendered.

🔧 Debug
advanced
2:00remaining
Why does this @ViewChild usage cause an error?

Examine this Angular component code:

import { Component, ViewChild, ElementRef } from '@angular/core';

@Component({
  selector: 'app-debug',
  template: `
Content
` }) export class DebugComponent { @ViewChild('myDiv') myDiv!: ElementRef; ngOnInit() { console.log(this.myDiv.nativeElement.textContent); } }

What error will occur and why?

Angular
import { Component, ViewChild, ElementRef } from '@angular/core';

@Component({
  selector: 'app-debug',
  template: `<div #myDiv>Content</div>`
})
export class DebugComponent {
  @ViewChild('myDiv') myDiv!: ElementRef;

  ngOnInit() {
    console.log(this.myDiv.nativeElement.textContent);
  }
}
ANo output, silent failure
BLogs 'Content' correctly
CSyntaxError due to missing semicolon
DError: Cannot read property 'nativeElement' of undefined
Attempts:
2 left
💡 Hint

Consider when @ViewChild properties are initialized relative to lifecycle hooks.

state_output
expert
2:30remaining
What is the final value of the input after this interaction with @ViewChild?

Given this Angular component:

import { Component, ViewChild, ElementRef, AfterViewInit } from '@angular/core';

@Component({
  selector: 'app-expert',
  template: `` 
})
export class ExpertComponent implements AfterViewInit {
  @ViewChild('inputRef') inputElement!: ElementRef;

  ngAfterViewInit() {
    this.inputElement.nativeElement.value = 'Changed';
  }

  changeValue() {
    this.inputElement.nativeElement.value = 'Clicked';
  }
}

If the user clicks a button that calls changeValue() after the view loads, what is the input's value?

Angular
import { Component, ViewChild, ElementRef, AfterViewInit } from '@angular/core';

@Component({
  selector: 'app-expert',
  template: `<input #inputRef type='text' value='Start' />` 
})
export class ExpertComponent implements AfterViewInit {
  @ViewChild('inputRef') inputElement!: ElementRef<HTMLInputElement>;

  ngAfterViewInit() {
    this.inputElement.nativeElement.value = 'Changed';
  }

  changeValue() {
    this.inputElement.nativeElement.value = 'Clicked';
  }
}
A"Clicked"
B"Changed"
C"Start"
Dundefined
Attempts:
2 left
💡 Hint

Think about the order of operations: initial value, lifecycle hook, and user interaction.