在ng-view上滚动时,无限滚动在AngularJS中不起作用

Dof*_*ofs 4 angularjs angularjs-directive

我使用的代码像这样创造AngularJS无限滚动效果.我尝试通过将可滚动容器(在本例中为ul)的内容移动到单独的html文件来重构某些代码,然后使用ng-view加载内容.

完成此操作后,scope.$apply(attr.whenScrolled);没有任何效果.该loadMore()方法根本不再被调用.

在将ul-content移动到单独的文件并将其动态加载后,我是否更改了有关范围的内容?

更新:这是代码:

App.directive('whenScrolled', function() {
return function(scope, element, attr) {
    var raw = element[0];

    // binding on element doesn't work so this is a temp fix
    $(document).bind('scroll', function() {
      var scrollPercentage = (($(window).scrollTop() + $(window).height()) / $(document).height()) * 100;

      if(scrollPercentage > 75 && !scope.in_progress && !scope.is_reached_end)
      {
        console.log('test')
        scope.$apply(attr.whenScrolled);
      }
    });
};
Run Code Online (Sandbox Code Playgroud)

});

App.config(['$routeProvider', function($routeProvider){
  $routeProvider.when('/', {
    templateUrl: 'views/offers.html',
    controller: 'OffersCntl'
  });
}]);
Run Code Online (Sandbox Code Playgroud)

风景:

<div class="tileContainer" ng-controller="OffersCntl">
    <h2>Something very important :)</h2>
    <div id="tiles" class="tiles" when-scrolled="loadMore()">
        <div ng-view></div>
    </div>
</div>  
Run Code Online (Sandbox Code Playgroud)

我有一个相当胖的控制器,我不想用它来判断帖子.它基本上有一个scope.loadMore方法.

Mat*_*ork 5

ng-include而不是ng-view.

http://jsfiddle.net/pvtpenguin/U7Bz9/540/

例如,在您的视图中:

 <div class="tileContainer" ng-controller="OffersCntl">
   <h2>Something very important :)</h2>
   <div id="tiles" class="tiles" when-scrolled="loadMore()">
     <div ng-include src="'offer.html'"></div>
   </div>
 </div>  
Run Code Online (Sandbox Code Playgroud)

  • 这不是一个真正的解决方案,因为ng-include不提供与ng-view相同的功能. (3认同)