簡體   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