lascoff
lascoff

Reputation: 1331

Using Angularjs how to reference an attribute from an element

Using angularjs

I am trying to reference an attribute from an element when a hotkey is pressed. In the boxes in the example, I would like to click alt-E and then get the value in an attribute on the element.

<body ng-controller="Ctrl" ng-keydown="down($event)">
<h1>Test HotKey!</h1>    
 <div><input data-box="65"></input></div>
 <div><input data-box="234"></input></div>
 <div><input data-box="9"></input></div>
 <div><input data-box="32"></input></div>
 <div><input data-box="13"></input></div>

The js looks like this

var myApp = angular.module('myApp', []);

myApp.controller('Ctrl', function($scope) {

   $scope.down = function($event) {

      console.log($event);
      console.log($scope);

      if($event.altKey === true ) {
          alert($event.target.attributes);
    }
  };

});

in this example when the alt-E is clicked I need the attribute value in data-box?

Upvotes: 0

Views: 214

Answers (1)

honzajscz
honzajscz

Reputation: 3107

What about using the scrElement property on the event to access the dom element and its attributes. E.g.:

 myApp.controller('Ctrl', function ($scope) {
        $scope.down = function ($event) {
            var elem = angular.element($event.srcElement);
            console.log(elem.attr("data-box"));
        };
    });

Upvotes: 1

Related Questions