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

4
我正在使用像这样的代码在AngularJS中创建无限滚动效果。我试图重构一些代码,将可滚动容器(在本例中是
    )的内容移动到单独的HTML文件中,然后使用ng-view加载内容。
    完成后,scope.$apply(attr.whenScrolled)不起作用了。 loadMore()方法不再被调用。
    当我将ul内容移动到单独的文件中并动态加载时,是否改变了作用域?
    更新: 以下是代码:
    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方法。


请发布代码以进行调试。 - Rajkamal Subramanian
2个回答

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>  

3
这不是一个真正的解决方案,因为ng-include不能像ng-view一样提供相同的功能。 - epegzz

0

该指令使用滚动偏移量为组件提供弹性,而不将其限制在固定高度上:

app.directive('whenScrolled', function($window, $timeout) {
  return {
    restrict: "A",
    link: function(scope, element, attr) {

      // bind the digest cycle to be triggered by the scroll event
      // when it exceeds a threshold
      angular.element($window).bind('scroll', function() {

        var supportPageOffset = window.pageXOffset !== undefined;
        var isCSS1Compat = ((document.compatMode || "") === "CSS1Compat");

        var scrollY = supportPageOffset ? window.pageYOffset : isCSS1Compat ? document.documentElement.scrollTop : document.body.scrollTop;

        var iScroll = element.prop('offsetTop') + element.prop('offsetHeight');
        var iScrooling =  scrollY + ( this.screen.height * 0.9 );

        //console.log(iScrooling+'/'+iScroll);

        if ( iScrooling >= iScroll ) {
          angular.element($window)[0].requestAnimationFrame(function(){
            // invoke the function passed into the 'whenScrolled' attribute
            scope.$apply(attr.whenScrolled);

          })
        }

      });
    }
  }
});

你的HTML代码:

<div class="tileContainer" ng-controller="OffersCntl">
   <h2>Something very important :)</h2>
   <div id="tiles" class="tiles" when-scrolled="loadMore()">
     <div ng-repeat="item in items">
       {{ item.id }}
     </div>
   </div>
</div> 

控制器,您可以将其替换为 Ajax 请求

$scope.items = [];

var counter = 0;
$scope.loadMore = function() {
    for (var i = 0; i < 5; i++) {
        $scope.items.push({id: counter});
        counter += 10;
    }
};

$scope.loadMore();

如果您需要支持旧版浏览器,可以添加此函数:
//requestAnimationFrame for old browsers

(function() {
  var lastTime = 0;
  var vendors = ['webkit', 'moz'];
  for(var x = 0; x < vendors.length && !window.requestAnimationFrame; ++x) {
    window.requestAnimationFrame =     window[vendors[x]+'RequestAnimationFrame'];
    window.cancelAnimationFrame = window[vendors[x]+'CancelAnimationFrame'] || window[vendors[x]+'CancelRequestAnimationFrame'];
  }

  if (!window.requestAnimationFrame)
    window.requestAnimationFrame = function(callback, element) {
      var currTime = new Date().getTime();
      var timeToCall = Math.max(0, 16 - (currTime - lastTime));
      var id = window.setTimeout(function() { callback(currTime + timeToCall); }, timeToCall);
      lastTime = currTime + timeToCall;
      return id;
    };

  if (!window.cancelAnimationFrame)
    window.cancelAnimationFrame = function(id) {
      clearTimeout(id);
    };
}());

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接