Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pass element ref to a method in angular2

I have to check if an element in a list is within the viewport or not.For this i'm using the angular2 plugin angular-inviewport.

What the plugin does is as soon as the card is withing the bottom of the window it return true.I want that the card should be in the center or at least somewhat near to the top of the window before i register the impression. For this i need the reference of the current element and compare it with the window height and Yoffset something like this(the last solution) .

Below is a small snippet from my code and the callback i have.

    <li class="" *ngFor="let data of dataArray; let i=index;">          <div id="data{{data.Id}}" snInViewport (inViewportChange)="handlerFunction($event,data)" class="card m-b-" style="height:auto;">         </div>    </li> 

even tried adding dynamic ref

<div #data_{{data.Id}} id="data{{data.Id}}" snInViewport (inViewportChange)="handlerFunction($event,data)" class="card m-b-" style="height:auto;"> 

Not sure if this is correct.

Inside the handlerFunction i want the div reference also.

How can i achieve this. Any suggestion,approach or guidance is welcome!

Thanks

like image 584
Shruti Nair Avatar asked Feb 09 '18 07:02

Shruti Nair


People also ask

How to inject an elementref in angular?

Ask for ElementRef in the constructor ( Angular Dependency injection ), the Angular will inject the reference element hosting the component/directive. For Example, in the following code, the variable hello refers to the HTML element div. hello is the Template Reference variable, which we can use in the Template.

How to get the reference of an htmlelement in angular?

We use the ViewChild to get the ElementRef of an HTML element in the component class. Angular also inject ElementRef of the Host element of the component or directive when you request for it in the constructor. In this tutorial, let us explore how to use ElementRef to get the reference of an HtmlElement & manipulate the DOM in Angular Applications.

How do I pass a string to a component in angular?

Passing a string value to a component from its class: Here, the message variable will get its value either from the constructor of the class or by using angular event bindings (user inputs). Example: It is the simplest way of passing a string to a component.

How to return data from app-Register component in angular?

The app component is a root component generated by Angular itself. We have added app-register component in app.component.html with the properties tableDataValues. Data will return from the app-register component by submitting values. It will hold and pass to a method that is submitted ($event).


1 Answers

Simply do :

Template Side :

<div #refEl (click)='yourFunc(refEl)'> 

Component Side :

yourFunc(el: HTMLElement){   console.log(el); // you can check the output in the console } 

------------------------ OR --------------------------

Template Side :

<div (click)='yourFunc($event.target)'></div> 

Component Side (Same as above):

WORKING DEMO

like image 87
Vivek Doshi Avatar answered Oct 11 '22 05:10

Vivek Doshi