3

For some reason my @ViewChild in my Angular 8 App does not work "undefined". I have defined it like this in html file:

<div class="container">
    <div class="row h-100">
      <kendo-splitter orientation="vertical">
        <kendo-splitter-pane>
          <kendo-splitter>
        <kendo-splitter-pane>
            ...
        </kendo-splitter-pane>
        <kendo-splitter-pane>
            <div>
                <myComponent #myComp ....> </<myComponent> // ViewChild id here
            </div>
        </kendo-splitter-pane>
    </kendo-splitter>
        </kendo-splitter-pane>
      </kendo-splitter>

in ts file I tried the following and all return undefined:

  1. @ViewChild(myComponent, { static: true }) child: myComponent;

  2. @ViewChild('myComp', { static: false }) child: myComponent;

  3. @ViewChild('myComp', {static:true}) child: ElementRef;

and I used ngAfterViewInit but still get undefined. Is it because myCompnent is wrapped by many html elements?

3
  • 1
    @ViewChild(myComponent, { static: true }) child: myComponent; this should ideally work, do you have a stackblitz link to demonstrate the issue? Commented Nov 30, 2019 at 0:10
  • Can you please retrieve the value in the ngOnInit() function in your myComponent component? Commented Nov 30, 2019 at 4:33
  • I'm know that sound as joke, but be sure the element is in the DOM (imagine you're using *ngIf in some condition) when you try to access it Commented Nov 30, 2019 at 14:44

1 Answer 1

1

Try

@ViewChild('myComp', { static: false }) child: myComponent;

Or if you are using

@ViewChild(myComponent, { static: true }) child: myComponent;

Make sure that myComponent is the component class name and not the selector (just to be clear because I cannot confirm it from the example code)

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.