Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular2: Get dimensions of DOM-Element (without ElementRef)

Tags:

dom

angular

I try to get dimensions of a DOM-Element (heigtht/width). As for security reasons (see https://angular.io/docs/ts/latest/guide/security.html) I don't want to directly acccess the DOM.

Does anyone have some other Idea?

like image 998
jackBot Avatar asked May 24 '26 15:05

jackBot


1 Answers

You could do it like this

import { Component } from '@angular/core';

@Component({
  selector: 'my-selector',
  template: '<div #myDiv (click)=getElementDimensions(myDiv)>Hello</div>'
})
export class AboutComponent {

  constructor() { }

  getElementDimensions(el: HTMLDivElement) {
    console.log(el.getBoundingClientRect())
  }

}
like image 106
koningdavid Avatar answered May 27 '26 03:05

koningdavid



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!