I don't exactly know what you need to do and or achieve but I will show you my solution for a similar use case.
My Problem was that I needed to scroll down a div on load. AfterViewChecked got executed multiple times but I needed to scroll down in this lifecycle. Thats what I did, maybe it helps you:
public scrollSuccessfull = false; // global variable
public ngAfterViewChecked(): void {
if (!this.scrollSuccessful) {
// gets executed as long as the scroll wasnt successfull, so the (successful) scroll only gets executed once
this.scrollSuccessful = this.scrollToBottom(this.internal ? this.internalChatDiv : this.externalChatDiv);
}
}
private scrollToBottom(element: ElementRef): boolean {
if (element) {
element.nativeElement.scrollTop = element.nativeElement.scrollHeight;
return element.nativeElement.scrollTop !== 0;
}
return false;
}
ngAfterViewInit
run multiple times/all the time? – Cathicathie