emka26
emka26

Reputation: 443

How to get position of iterated element angularjs?

I have iterated dropdown and I would like to know what is the position of each of these dropdowns when I click on it.

<div class="row" ng-repeat="animal in vm.animals">
  {{animal}}
  <btn-group uib-dropdown>
    <button class="btn btn-secondary btn-sm uib-dropdown-toggle
            ng-click="vm.checkPosition($event)">{{vm.test}}
    </button>
    <div class="dropdown-menu" uib-dropdown-menu>
      <a class="dropdown-item" ng-repeat="dog in vm.dogs">
        {{dog}}
      </a>
  </btn>
function checkPosition($event) {
  // I tried use here $event.target.el.getBoundingClientRect().top, but this 
  // position isnt element which I clicked, but propably first element with
  // this class
}

Upvotes: 0

Views: 24

Answers (2)

Jazib
Jazib

Reputation: 1381

If you mean the index of the array you can use $index as Scorpioo590 mentioned. which in angularjs will give you the item's index in the array that is used in the ng-repeat you can use it like this:

ng-click="vm.checkPosition($event, $index)"
function checkPosition($event, index) {
 console.log(index);
}

Upvotes: 0

jSebesty&#233;n
jSebesty&#233;n

Reputation: 1806

As the docs describe, you have access to certain variables inside of your ng-repeat.

For example $first is true only for the first element, $last only for the last one. What you are looking for is $index

Upvotes: 1

Related Questions