加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

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

发布时间:2020-12-17 17:18:13 所属栏目:安全 来源:网络整理
导读:我使用像 this这样的代码在AngularJS中创建无限滚动效果.我尝试通过将可滚动容器的内容(在本例中为ul)移动到单独的html文件来重构某些代码,然后使用ng-view加载内容. 完成此操作后,范围.$apply(attr.whenScrolled);没有任何影响.不再调用loadMore()方法. 在
我使用像 this这样的代码在AngularJS中创建无限滚动效果.我尝试通过将可滚动容器的内容(在本例中为ul)移动到单独的html文件来重构某些代码,然后使用ng-view加载内容.

完成此操作后,范围.$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);
      }
    });
};

});

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

风景:

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

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

解决方法

使用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>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读