0

Can I get the ElementRef from doWidth()?

<div [style.left.px]="doWidth(maybeThisElementRef)">
</div>

Rather than turning the div into a component?

el_pup_le
  • 11,711
  • 26
  • 85
  • 142

2 Answers2

2

Just use template reference variable like:

<div #el [style.left.px]="doWidth(el)">

See also

yurzui
  • 205,937
  • 32
  • 433
  • 399
1

In addition to yurzui's answer, you can also reference it in your components as this:

<div #el [style.left.px]="doWidth()">


  import { ViewChild } from '@angular/core';
  ....
  @ViewChild('el') private el;
  ...
  doWidth(){
    this.el.value="blabla" //or something else 
  }
Vega
  • 27,856
  • 27
  • 95
  • 103