What is the read parameter in @ViewChild for

Angular

Angular Problem Overview


Need help to understand meaning of {read: ViewContainerRef} in following statement.

@ViewChild('myname', {read: ViewContainerRef}) target;

Angular Solutions


Solution 1 - Angular

There can be several instances of various types associated with the element tag with the #myname template variable.

For each element there is an ElementRef and ViewContainerRef (maybe others from components or directives applied to that tag).

If the element is a component, then there is the component instance.

There can also be one or several directives applied to the element

With {read: SomeType} you tell what type should be returned from the element with the #myname template variable.

If you don't provide the read parameter, @ViewChild() returns the

  • ElementRef instance if there is no component applied, or the
  • component instance if there is.
  • If you want to get something different you need to explicitly specify using read.

See also https://stackoverflow.com/questions/32693061/angular-2-typescript-get-hold-of-an-element-in-the-template

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionPankaj KapareView Question on Stackoverflow
Solution 1 - AngularGünter ZöchbauerView Answer on Stackoverflow