繁体   English   中英

角度js滚动指令不起作用

[英]angular js scrolling directive not working

我正在尝试做angular(1.3.14)指令来处理像这样的元素上的滚动事件

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

app.directive("scroll", function ($window) {
    return {
      restrict: 'A',
      link: function(scope, element, attrs) {
          console.log(element.className); // return 'undefined'
          element.on('scroll', function(e) {
            console.log('scroll'); //not working
          });
          element.on('click', function(e) {
            console.log('click'); //working
          });
      }
    }
});

我的问题是滚动事件不会触发。 其他所有事件(例如单击)均正常运行,但滚动不正常。 另外,当我尝试获取元素的类时,我得到“未定义”,并且我的元素具有类。 它是html:

<body ng-app="myApp" ng-controller="myCtrl" ng-keydown="keyListener($event)">
    <section class="dark content second" scroll="">         
    </section>
</body>

我不知道这里有什么问题。

您的指令是正确的,我在您的部分中对内部div进行了测试,并使用了一些类使其可滚动

<section class="dark content second" scroll="">
  Hi         
  <div class="internal">
    Something
  </div>
</section>

的CSS

    .second{
      background-color: red;
      max-height: 150px;
     overflow-y:scroll;
    }

   .internal{
      height: 200px;
    }

该活动非常完美! 您只需要使<section>可滚动或在body / html标记中应用指令即可。 这是我已经测试过的Plunker示例http://plnkr.co/edit/hp2BbnLeGjtwIbfi2mqZ?p=preview

尝试这个

     console.log(attrs.class);
     element.bind('scroll', function() {
        console.log('scroll');
     });

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM