Skip to content

Instantly share code, notes, and snippets.

@teomanofficial
Created November 27, 2023 22:35
Show Gist options
  • Save teomanofficial/06f59c7d93307f314f0c13c81f2922cb to your computer and use it in GitHub Desktop.
Save teomanofficial/06f59c7d93307f314f0c13c81f2922cb to your computer and use it in GitHub Desktop.
Angular AfterRender Hook
@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