Created
October 17, 2019 08:21
-
-
Save michelepatrassi/04b60ec7b5b800795b98f20da8270b36 to your computer and use it in GitHub Desktop.
Angular MutationObserver
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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