Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bind to size of element (div)

Tags:

angular

I have a component with Width and Height properties. How can I bind component's view height and width to these properties?

I need them to be updated whenever component size changes too, i.e. by re-sizing the browser window.

like image 458
Jarek Avatar asked Nov 21 '15 10:11

Jarek


1 Answers

Use (window:resize)="onResize($event)" to listen to global events. You can use window, document or body as targets. See this plunk

@Component({
  selector: 'my-app',
  template: `
    <div 
      class="square"
      [style.width.px]="width" 
      [style.height.px]="height"
      (window:resize)="onResize($event)"
    ></div>
  `
})
export class App {
  width = 100;
  height = 100;

  onResize(event) {
    this.width += 100;
    this.height += 100;
  }
}
like image 182
alexpods Avatar answered Sep 29 '22 01:09

alexpods