How to retrieve the clicked ElementId in angularjs?

a 夏天 提交于 2019-11-27 20:42:09

问题


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.


回答1:


I solved this:

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

$scope.ShowId = function(event)
{
   alert(event.target.id);
};



回答2:


<button data-id="101" ng-click="showDetail($event)">click Me</button>

$scope.showDetail = function(event)
{
  console.log($(event.target).attr("data-id"));
}


来源:https://stackoverflow.com/questions/31955930/how-to-retrieve-the-clicked-elementid-in-angularjs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!