Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a span tag like below which call a function in the controller when clicked.

HTML

<div class="row" ng-repeat="event in events">
    <div class="col-lg-1 text-center"><span class="glyphicon glyphicon-trash" data={{event.id}} ng-click="deleteEvent()"></span></div>
</div>

Controller

$scope.deleteEvent=function(){
    console.log(this);
}

I need to get the value in data attribute in the controller function. Itries using this keyword and $event both dint work.

Please help.

share|improve this question

2 Answers

up vote 4 down vote accepted

Try passing it directly to the ng-click function:

<div class="col-lg-1 text-center">
    <span class="glyphicon glyphicon-trash" data="{{event.id}}"
          ng-click="deleteEvent(event.id)"></span>
</div>

Then it should be available in your handler:

$scope.deleteEvent=function(idPassedFromNgClick){
    console.log(idPassedFromNgClick);
}

Here's an example

share|improve this answer
 
Actually this whole div comes inside ng-repeat. So i cannot use event.id inside ng-click. I think. –  Saravanan Aug 3 at 4:38
 
Check out the jsFiddle link I just added to my answer for an example of using a parameter to ngClick within ngRepeat –  jandersen Aug 3 at 4:42
 
Thanks a lot. I did try this yesterday and spent more than 3 hours. It dint work. I might have made some mistake. Anyways now it works and thanks for the fiddle. It helped. –  Saravanan Aug 3 at 4:47

You can pass the $event object to ng-click to access the event properties. As an example:

<a ng-click="clickEvent($event)" class="exampleClass" id="exampleID" data="exampleData" href="">Click Me</a>

Within your clickEvent() function you can access the data value like this:

var dataValue = obj.srcElement.attributes.data.nodeValue;

Which would return exampleData.

Here's a full jsFiddle.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.