0

Trying to implement a simple keypress event on a div but somehow nothing happens. I have read at some places that I need tabindex="0", however problem still prevails. Here is my html:

<body  ng-app="myModule">

<div ng-controller="myCtrl" movement tabindex="1" ng-keypress="iterate($event)">
<div ng-repeat="entity in entities" entity-position="entity"></div>
<div class="table">
    <div>
        <select selected='name' ng-model="selected" id="select">
            <option value="name" >First</option>
            <option value="id">Second</option>
            <option value="id">Third</option>
        </select>
    </div>
    <table id="table">
        <tr ng-repeat="item in cars | orderBy:selected"  ng-click="go(item)">
            <td>{{item.plate}}</td>
            <td>{{item.name}}</td>
            <td>{item.price}}</td>
        </tr>
    </table>
</div>

"movement" is a custom directive with it's own controller, I'm guessing the problem is connected to this, but I'm not sure.

This is inside the movement directive's controller (never logs anything):

$scope.iterate = function($event){
      console.log('iterate')    
}

Thank you for your help.
Edit: Added directive

    app.directive('movement', function() {
    return {
    controller: function($scope, animFrame) {
    var width = window.innerWidth;
    var height = window.innerHeight;
    var speed = .5;
    var lastTime = new Date().getTime();

    $scope.entities = [];

    $scope.changeCount = function(count) {
        while(count>0) {
            $scope.entities.push({
                x: width/2 * Math.random()+100,
                y: height/2 * Math.random()+100,
                velX: 0.2,//speed * Math.random()/2,
                velY: 0.1,//speed * Math.random()/3
            });
            count--;
        }
        while(count<0) {
            $scope.entities.pop();
            count++;
        }
    }


    $scope.tick = function($event){
      //console.log('tick')
      var now = new Date().getTime(),
          delay = now - lastTime,
          entities = $scope.entities;

      for(var i=0; i<entities.length; i++) {
          var b = entities[i];
          b.x += delay * b.velX;
          b.y += delay * b.velY;
          if (b.x < 0) { b.x *= -1; b.velX *= -1;}
          if (b.y < 0) { b.y *= -1; b.velY *= -1; }
          if (b.x > width) { b.x = 2*width - b.x; b.velX *= -1;}
          if (b.y > height) { b.y = 2*height - b.y; b.velY *= -1; }
      }
      lastTime = now;
      animFrame($scope.tick);
    }

    $scope.changeCount(1);
    $scope.tick();
  }
 };
});
George Kagan
  • 5,913
  • 8
  • 46
  • 50
Johnny
  • 13
  • 1
  • 7

0 Answers0