Created
November 27, 2023 22:35
-
-
Save teomanofficial/06f59c7d93307f314f0c13c81f2922cb to your computer and use it in GitHub Desktop.
Angular AfterRender Hook
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({ | |
selector: 'my-cmp', | |
template: ` | |
<span #content>{{ ... }}</span> | |
<span #tooltip>{{ ... }}</span> | |
`, | |
}) | |
export class MyComponent { | |
@ViewChild('content') contentRef: ElementRef; | |
@ViewChild('tooltip') tooltipRef: ElementRef; | |
constructor() { | |
afterRender(() => { | |
const { top, left } = this.contentRef.nativeElement.getBoundingClientRect(); | |
this.tooltipRef.nativeElement.style.top = top; | |
this.tooltipRef.nativeElement.style.left = left; | |
}, { phase: AfterRenderPhase.Read }); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment