@viewChild not working - cannot read property nativeElement of undefined

后端 未结 8 1938
孤街浪徒
孤街浪徒 2020-12-05 06:00

I\'m trying to access a native element in order to focus on it when another element is clicked (much like the html attribute \"for\" - for cannot be used on elements of th

8条回答
  •  醉酒成梦
    2020-12-05 06:40

    You'll also get this error if your target element is inside a hidden element. If this is your HTML:

    Hello World

    Your @ViewChild('sp') sp will be undefined.

    Solution

    In such a case, then don't use *ngIf.

    Instead use a class to show/hide your element being hidden.

    ...

提交回复
热议问题