2014-10-07 51 views
0

我知道如何在純JS中滾動時檢測文檔結束,我的問題是 - 如何在AngularJS中實現該功能,我知道我應該將滾動事件附加到兩個$window$document,我的問題是關於在哪裏實施行爲?,指令?,服務?,如果任何人都可以告訴我什麼是正確的方式來實現在AngularJS kind of detection我會非常感謝。在Angular JS中檢測滾動到文檔結束的實現

+0

你應該去看看這個模塊的源代碼。 http://binarymuse.github.io/ngInfiniteScroll/。它在滾動檢測方面做了很多工作。 – 2014-10-07 18:07:37

+0

@GordonBockus感謝您的鏈接。 – 2014-10-07 18:19:54

回答

1

根據你在做什麼,它可以在地方的組合。通常,使用DOM操作打交道時(這是我認爲會發生),你應該用一個指令 - 是這樣的:

app.directive("scrollDetector", ["$document", "$window", function($document, $window) { 
    return { 
     restrict: "A", 
     link: function(scope, elem, attrs) { 
      angular.element($window).bind("scroll", function() { 
       //scroll logic here 
      }); 
     } 
    } 
}]); 

,然後實現scroll-detector指令。 (例如:<div scroll-detector></div>

+0

謝謝,現在我知道如何。 – 2014-10-07 18:20:37

2

經過長時間的苦苦掙扎後,偶然發現圖書館:http://binarymuse.github.io/ngInfiniteScroll/documentation.html

根據您的使用情況,你可以這樣做:

<div infinite-scroll="addMoreItems()"> 
    <div ng-repeat="item in items">Item number {{$index}}: {{$item}}</div> 
</div> 

因爲它可以讓你這個連接到任何div-你幾乎可以做你想要的任何功能。

+0

謝謝,我會看看。 – 2015-09-29 08:02:19

+0

更新後的鏈接:http://sroze.github.io/ngInfiniteScroll/documentation.html – yarl 2016-10-22 00:47:12