@viewChild not working - cannot read property nativeElement of undefined

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

    The accepted answer is correct in all means and I stumbled upon this thread after I couldn't get the Google Map render in one of my app components.

    Now, if you are on a recent angular version i.e. 7+ of angular then you will have to deal with the following ViewChild declaration i.e.

    @ViewChild(selector: string | Function | Type, opts: {
    read?: any;
    static: boolean;
    })
    

    Now, the interesting part is the static value, which by definition says

    • static - True to resolve query results before change detection runs

    Now for rendering a map, I used the following ,

    @ViewChild('map', { static: true }) mapElement: any;
      map: google.maps.Map;
    

提交回复
热议问题