Reputation: 459
I'm trying to get the attribute value of element but return undefined.
html
<button id="myId" ng-click="hi($event)">click me</button>
js
$scope.hi = function (e) {
var elem = angular.element(e.srcElement);
alert(elem.attr("id"));
}
why i get undefined?
Upvotes: 0
Views: 169
Reputation: 4870
Change e.srcElement
to e.target
example:
$scope.hi = function (e) {
var elem = angular.element(e.target);
alert(elem.attr("id"));
}
OR u can use
$scope.hi = function (e) {
alert(e.target.id);
}
Upvotes: 1
Reputation: 3327
$event
will pass the click event to your function.
For working with the element itself, you should use a directive.
app.directive('mydirectiver', function() {
return {
link: function(scope,element,attrs){
//element will point to the element
//attrs will point to the elements attributes
}
};
});
I strongly recommend the lecture of the Angular docs on directives. They are a little confusing in the beginning, but you should be able to wrap your head around the concept very fast and benefit from their power.
Upvotes: 0