@@ -522,6 +522,7 @@ function VirtualRepeatController($scope, $element, $attrs, $browser, $document,
522522 this . $attrs = $attrs ;
523523 this . $browser = $browser ;
524524 this . $document = $document ;
525+ this . $mdUtil = $mdUtil ;
525526 this . $rootScope = $rootScope ;
526527 this . $$rAF = $$rAF ;
527528
@@ -761,16 +762,6 @@ VirtualRepeatController.prototype.virtualRepeatUpdate_ = function(items, oldItem
761762 this . container . setScrollSize ( itemsLength * this . itemSize ) ;
762763 }
763764
764- var cleanupFirstRender = false , firstRenderStartIndex ;
765- if ( this . isFirstRender ) {
766- cleanupFirstRender = true ;
767- this . isFirstRender = false ;
768- firstRenderStartIndex = this . $attrs . mdStartIndex ?
769- this . $scope . $eval ( this . $attrs . mdStartIndex ) :
770- this . container . topIndex ;
771- this . container . scrollToIndex ( firstRenderStartIndex ) ;
772- }
773-
774765 // Detach and pool any blocks that are no longer in the viewport.
775766 Object . keys ( this . blocks ) . forEach ( function ( blockIndex ) {
776767 var index = parseInt ( blockIndex , 10 ) ;
@@ -822,16 +813,25 @@ VirtualRepeatController.prototype.virtualRepeatUpdate_ = function(items, oldItem
822813 this . blocks [ maxIndex ] && this . blocks [ maxIndex ] . element [ 0 ] . nextSibling ) ;
823814 }
824815
825- // DOM manipulation may have altered scroll, so scroll again
826- if ( cleanupFirstRender ) {
827- this . container . scrollToIndex ( firstRenderStartIndex ) ;
828- }
829816 // Restore $$checkUrlChange.
830817 this . $browser . $$checkUrlChange = this . browserCheckUrlChange ;
831818
832819 this . startIndex = this . newStartIndex ;
833820 this . endIndex = this . newEndIndex ;
834821
822+ if ( this . isFirstRender ) {
823+ this . isFirstRender = false ;
824+ var firstRenderStartIndex = this . $attrs . mdStartIndex ?
825+ this . $scope . $eval ( this . $attrs . mdStartIndex ) :
826+ this . container . topIndex ;
827+
828+ // The first call to virtualRepeatUpdate_ may not be when the virtual repeater is ready.
829+ // Introduce a slight delay so that the update happens when it is actually ready.
830+ this . $mdUtil . nextTick ( function ( ) {
831+ this . container . scrollToIndex ( firstRenderStartIndex ) ;
832+ } . bind ( this ) ) ;
833+ }
834+
835835 this . isVirtualRepeatUpdating_ = false ;
836836} ;
837837
0 commit comments