Hi While using ElementRef in Viewchild() I am facing an error. Please see the below error and let me know what is an issue.
I also used ngAfterViewInit() but this error can not be resolved.
import { Component, OnInit,ViewChild,ElementRef,Input } from '@angular/core';
export class SearchInsightEverythingComponent implements OnInit {
@ViewChild("svgNetwork",{read: ElementRef}) svgNetworkContainer: ElementRef;
ngOnInit(){
draw();
}
draw(){
var container = this.svgNetworkContainer.nativeElement;
console.log("svgNetworkContainer = " + JSON.stringify(this.svgNetworkContainer));
}
}
.HTML
<div id="viz" #svgNetwork style="height: calc(100vh - 174px);">
<img src="../../../assets/exxon/graph.png">
</div>
Error
core.js:1440 ERROR TypeError: Cannot read properties of undefined (reading 'nativeElement')
at SearchInsightEverythingComponent.drawNetwork (search-everything-insight.component.ts:505:46)
at SafeSubscriber.eval [as _next] (search-everything-insight.component.ts:697:16)
at SafeSubscriber.__tryOrUnsub (Subscriber.js:240:1)
at SafeSubscriber.next (Subscriber.js:187:1)
at Subscriber._next (Subscriber.js:128:1)
at Subscriber.next (Subscriber.js:92:1)
at MapSubscriber._next (map.js:85:1)
at Subscriber.next (Subscriber.js:92:1)
at FilterSubscriber._next (filter.js:90:1)
at Subscriber.next (Subscriber.js:92:1)