There seems to be an issue with my variables resetting after successfully using them in ngAfterViewInit().
I have a few @ViewChild and regular variables that are utilized or set in ngAfterViewInit. However, when certain events that I added post-initialization are triggered, these same variables appear as undefined
.
Here is a slightly simplified version of the code:
// Variable declaration
@ViewChild('imagearea') imageArea: ElementRef;
dragBottomRight: HTMLElement;
ngAfterViewInit() {
this.dragBottomRight = this.imageArea.nativeElement.querySelector('.br');
this.imageArea.nativeElement.addEventListener("mousedown", this.dragStart);
// The elements are logged correctly at this point
console.log(this.imageArea, this.dragBottomRight);
}
dragStart(e: MouseEvent) {
// This logs: undefined, undefined
console.log(this.imageArea, this.dragBottomRight);
}
I've attempted setting the variables to static, and a similar code structure appears to work for my colleague.
Any assistance would be greatly appreciated.