1

I'm studing Angular. How is better to configure the new Angular 8 view child?

@ViewChild('example', {read: ElementRef, static: false})
public example: ElementRef;

or

@ViewChild('example', {read: ElementRef, static: true})
public example: ElementRef;

Which is better? When should I use static:true vs static:false?

  • Does this answer your question? [How should I use the new static option for @ViewChild in Angular 8?](https://stackoverflow.com/questions/56359504/how-should-i-use-the-new-static-option-for-viewchild-in-angular-8) – SAM Jun 24 '20 at 08:56

2 Answers2

2

You can go for the following:

  • { static: true } needs to be set when you want to access the ViewChild in ngOnInit.

  • { static: false } can only be accessed in ngAfterViewInit.

Chris
  • 806
  • 1
  • 10
  • 17
0

{ static: true } is used to resolve query results before change detection runs,

{ static: true } is used to resolve after change detection.

What is change detection in angular ?

Angular can detect when component data changes, and then automatically re-render the view to reflect that change.

Hope this could help.

Sam
  • 108
  • 1
  • 7