Skip to content

Instantly share code, notes, and snippets.

@michelepatrassi
Created October 17, 2019 08:21
Show Gist options
  • Save michelepatrassi/04b60ec7b5b800795b98f20da8270b36 to your computer and use it in GitHub Desktop.
Save michelepatrassi/04b60ec7b5b800795b98f20da8270b36 to your computer and use it in GitHub Desktop.
Angular MutationObserver
Component code
....
ngAfterViewInit() {
const config = { attributes: false, childList: true, subtree: true };
this.messageTreeObserver = new MutationObserver((mutationList, observer) => {
for (const mutation of mutationList) {
if (mutation.type === 'childList') {
// Do action here!
}
}
});
this.messageTreeObserver.observe(this.chatContent.nativeElement, config);
}
...
ngOnDestroy() {
if (this.messageTreeObserver) {
this.messageTreeObserver.disconnect(); //remember to disconnect the obvserver!
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment