23

I have the following line:

<a href="#" id="12345" data-ng-click="ShowId()">

and in my controller I have:

$scope.ShowId = function(){
     alert('clicked element id in here: 12345');
};

How can I access in my controller ShowId function the id of the clicked element, in my case 12345?

Notice the bind is not within the ng-repeat so I can access the item id or something like that.

Sharath
  • 691
  • 8
  • 23
user2818430
  • 5,853
  • 21
  • 82
  • 148

2 Answers2

35

I solved this:

<a href="#" id="12345" data-ng-click="ShowId($event)">   

$scope.ShowId = function(event)
{
   alert(event.target.id);
};
user2818430
  • 5,853
  • 21
  • 82
  • 148
  • 1
    it is not working for me, I have added id={{$index+1}} and same function I have writen in JS but `event.target.id` is showing blank in console. is there any other way ? – ojus kulkarni Nov 23 '16 at 19:09
5
<button data-id="101" ng-click="showDetail($event)">click Me</button>

$scope.showDetail = function(event)
{
  console.log($(event.target).attr("data-id"));
}
Tunaki
  • 132,869
  • 46
  • 340
  • 423
nano dev
  • 335
  • 4
  • 6