Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
162
rated 0 times [  165] [ 3]  / answers: 1 / hits: 49589  / 10 Years ago, sat, december 20, 2014, 12:00:00

In jQuery we can get position of an object with this:



$(this).position().left;


How can we get the current element position with AngularJS?


More From » jquery

 Answers
6

You can use prop() from angular jqlite. to select an element you can use querySelector() that returns first matching element or querySelectorAll() that return all matching element



angular.element(document.querySelector('yourelement')).prop('offsetLeft');


or if your this is valid dom element



angular.element(this).prop('offsetLeft');


Example for div



you can reference the DOM object with $event in html and pass it to the function on controller



<div ng-click=myfunction($event)></div>


in controller



$scope.myfunction = function($event){
console.log(angular.element($event.target).prop('offsetLeft'));

}


Demo





app = angular.module('test',[]);
app.controller('testctrl',function($scope){


$scope.myfunction = function($event){
console.log($event);
off =angular.element($event.target).prop('offsetLeft');
angular.element($event.target).text(off)

}

});

.divvy{position:absolute; left:60px;
width:100px; background:red;
}

   

<script src=https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js></script>
<div ng-controller=testctrl ng-app=test>
<div class=divvy ng-click=myfunction($event) >Click to see position</div>
</div>




[#68427] Thursday, December 18, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
kyla

Total Points: 77
Total Questions: 108
Total Answers: 111

Location: Grenada
Member since Mon, May 8, 2023
1 Year ago
;