el_pup_le
el_pup_le

Reputation: 12179

Angular 2 getting an element ref for function

Can I get the ElementRef from doWidth()?

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

Rather than turning the div into a component?

Upvotes: 0

Views: 1053

Answers (2)

Vega
Vega

Reputation: 28708

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 
  }

Upvotes: 1

yurzui
yurzui

Reputation: 214095

Just use template reference variable like:

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

See also

Upvotes: 2

Related Questions