@viewChild not working - cannot read property nativeElement of undefined

后端 未结 8 1905
孤街浪徒
孤街浪徒 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:45

    @ViewChild('keywords-input') keywordsInput; doesn't match id="keywords-input"

    id="keywords-input"
    

    should be instead a template variable:

    #keywordsInput
    

    Note that camel case should be used, since - is not allowed in template reference names.

    @ViewChild() supports names of template variables as string:

    @ViewChild('keywordsInput') keywordsInput;
    

    or component or directive types:

    @ViewChild(MyKeywordsInputComponent) keywordsInput;
    

    See also https://stackoverflow.com/a/35209681/217408

    Hint:
    keywordsInput is not set before ngAfterViewInit() is called

提交回复
热议问题