AngularJS - get element attributes values
Solution 1:
Since you are sending the target element to your function, you could do this to get the id:
function doStuff(item){
var id = item.attributes['data-id'].value; // 345
}
Solution 2:
You just can use doStuff($event) in your markup and get the data-attribute values via currentTarget.getAttribute("data-id")) in angular. HTML:
<div ng-controller="TestCtrl">
<button data-id="345" ng-click="doStuff($event)">Button</button>
</div>
JS:
var app = angular.module("app", []);
app.controller("TestCtrl", function ($scope) {
$scope.doStuff = function (item) {
console.log(item.currentTarget);
console.log(item.currentTarget.getAttribute("data-id"));
};
});
Forked your initial jsfiddle: http://jsfiddle.net/9mmd1zht/116/
Solution 3:
the .data()
method is from jQuery. If you want to use this method you need to include the jQuery library and access the method like this:
function doStuff(item) {
var id = $(item).data('id');
}
I also updated your jsFiffle
UPDATE
with pure angularjs and the jqlite you can achieve the goal like this:
function doStuff(item) {
var id = angular.element(item).data('id');
}
You must not access the element with []
because then you get the pure DOM element without all the jQuery or jqlite extra methods.