AngularJS niether ng-keypress或custom指令正在运行

时间:2016-11-13 12:25:59

标签: angularjs

尝试在div上实现一个简单的按键事件但不知何故没有任何反应。我在某些地方读过我需要的tabindex="0",但问题仍然存在。这是我的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>

“移动”是一个带有自己的控制器的自定义指令,我猜这个问题与此有关,但我不确定。

这是在运动指令的控制器内部(从不记录任何内容):

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

感谢您的帮助 修改:添加了指令

    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();
  }
 };
});

0 个答案:

没有答案