Angularjs ng-click: how to get `this` data?
Asked Answered
H

4

47

Let say I have this item in the list with angular ng-click event.

<a data-id='102' ng-click='delete()'>Delete</a>

How can I get the data/ info if this then?

        $scope.delete = function() {

             var id = $(this).attr('data-id');
             console.log(id); // I want to get 102 as the result
             if (confirm('Are you sure to delete?')) {
                $('#contactsGrid tr[data-id="' + id + '"]').hide('slow');

            }
         };
Hombre answered 28/12, 2013 at 12:15 Comment(3)
Don't know why this got downvotes. It's a valid question for someone new to the AngularJS approach, and it's expected that someone new to AngularJS would be looking to StackOverflow for guidance.Bannasch
@ChristopherParker +1 not to mention that it's a really well written question compared to many...!Humpy
I have to same question. I am new to AngularJS and JQuery and interaction between them is not so easy to understand.Crumpled
H
49

The right solution will be is to pass the id as an parameter to the delete function like

<a data-id='102' ng-click='delete(102)'>Delete</a>

then

    $scope.delete = function(id) {
         console.log(id); // I want to get 102 as the result
         if (confirm('Are you sure to delete?')) {
            $('#contactsGrid tr[data-id="' + id + '"]').hide('slow');

        }
     };

This should not be done, but just to demonstrate

Inside ng-click you can get the event using $event, so

<a data-id='102' ng-click='delete($event)'>Delete</a>

then

$scope.delete = function (e) {
    var id = $(e.target).data('id');
    console.log(id); // I want to get 102 as the result
};

Demo: Fiddle

Huge answered 28/12, 2013 at 12:16 Comment(7)
Yep, best to think about delete() independently of how its called. Happens to be from a click, but in the general case it wouldn't. Note though: presumably this <a> tag is inside of some kind of ng-repeat? If so, the iterated item is in scope and would be accessible via this.Pili
Thanks I was looking for this answer. But why not to use the $event ?Exterminate
bump. also curious as to why not use $event?Mccallion
@Mccallion sorry, what about $event? and how do you suggest to use itHuge
I'm using your solution. that works fine for me. however i am curious why you write "This should not be done, but just to demonstrate.". I'm curious why one should not use the $event solution.Mccallion
This should not be done because of the AngularJS concept if we believe this post : linkCrumpled
Nice answer this is resole to my problum thank @ArunPJohnyCoadjutant
M
4

To access clicked link tag attributes

In jQuery,

<a class='test' data-id='102' ng-click='delete(102)'>Delete</a>

on click of above link is handled as

$('.test').click(function(){
   console.log($(this).attr('data-id')); 
});

Demo code for jQuery : fiddle

In Angularjs,

<a class='test' data-id='102' ng-click='delete($event)'>Delete</a>

on click of above link is handled as

$scope.delete = function (e) {
    console.log($(e.currentTarget).attr("data-id")); 
 }

Demo code for Angularjs : fiddle

Macrae answered 5/5, 2015 at 9:19 Comment(1)
Wouldn't you get $ is not defined?Dayton
L
2

You can also access the event data for Jquery in angular using:

  $scope.myClickedEvent = function(clickEvent) {
    $scope.clickEvent = simpleKeys(clickEvent);
    angular.element(clickEvent.currentTarget);
    console.log(angular.element(clickEvent.currentTarget).text());
   /*
   * return a copy of an object with only non-object keys
   * we need this to avoid circular references
   */
   function simpleKeys (original) {
    return Object.keys(original).reduce(function (obj, key) {
      obj[key] = typeof original[key] === 'object' ? '{ ... }' : original[key];
      return obj;
    }, {});
  }
};

Your clicked element should contain an ng-click like this

ng-click="myClickedEvent($event)"
Liable answered 8/2, 2016 at 10:36 Comment(0)
N
0

If for some other reason, you still need to access the element here's how I did it :

<span ng-click="selectText($event)"></span>

and in the controller

$scope.selectText = function(event) {
  var element = event.currentTarget; // returns the span DOM Element
  // Now you can access its dataset just like in plain old JS
  // In my case it was for selecting the content of a tag on click anywhere on the tag
};
Nerin answered 9/11, 2016 at 15:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.