@@ -111,7 +111,7 @@ function VirtualRepeatContainerController($$rAF, $scope, $element, $attrs) {
111
111
/** Called by the md-virtual-repeat inside of the container at startup. */
112
112
VirtualRepeatContainerController . prototype . register = function ( repeaterCtrl ) {
113
113
this . repeater = repeaterCtrl ;
114
-
114
+
115
115
angular . element ( this . scroller )
116
116
. on ( 'scroll wheel touchmove touchend' , angular . bind ( this , this . handleScroll_ ) ) ;
117
117
} ;
@@ -192,10 +192,17 @@ VirtualRepeatContainerController.prototype.getScrollOffset = function() {
192
192
return this . scrollOffset ;
193
193
} ;
194
194
195
+ /**
196
+ * Scrolls to a given scrollTop position.
197
+ * @param {number } position
198
+ */
199
+ VirtualRepeatContainerController . prototype . scrollTo = function ( position ) {
200
+ this . scroller [ this . isHorizontal ( ) ? 'scrollLeft' : 'scrollTop' ] = position ;
201
+ this . handleScroll_ ( ) ;
202
+ } ;
195
203
196
204
VirtualRepeatContainerController . prototype . resetScroll = function ( ) {
197
- this . scroller [ this . isHorizontal ( ) ? 'scrollLeft' : 'scrollTop' ] = 0 ;
198
- this . handleScroll_ ( ) ;
205
+ this . scrollTo ( 0 ) ;
199
206
} ;
200
207
201
208
@@ -293,6 +300,13 @@ function VirtualRepeatController($scope, $element, $attrs, $browser, $document)
293
300
// getComputedStyle?
294
301
/** @type {number } Height/width of repeated elements. */
295
302
this . itemSize = $scope . $eval ( $attrs . mdItemSize ) ;
303
+
304
+ /** @type {boolean } Whether this is the first time that items are rendered. */
305
+ this . isFirstRender = true ;
306
+
307
+ /** @type {number } Most recently seen length of items. */
308
+ this . itemsLength = 0 ;
309
+
296
310
/**
297
311
* Presently rendered blocks by repeat index.
298
312
* @type {Object<number, !VirtualRepeatController.Block }
@@ -371,6 +385,12 @@ VirtualRepeatController.prototype.getItemSize = function() {
371
385
*/
372
386
VirtualRepeatController . prototype . virtualRepeatUpdate_ = function ( items , oldItems ) {
373
387
var itemsLength = items ? items . length : 0 ;
388
+ var lengthChanged = false ;
389
+
390
+ if ( itemsLength !== this . itemsLength ) {
391
+ lengthChanged = true ;
392
+ this . itemsLength = itemsLength ;
393
+ }
374
394
375
395
// If the number of items shrank, scroll up to the top.
376
396
if ( this . items && itemsLength < this . items . length && this . container . getScrollOffset ( ) !== 0 ) {
@@ -385,7 +405,16 @@ VirtualRepeatController.prototype.virtualRepeatUpdate_ = function(items, oldItem
385
405
}
386
406
387
407
this . parentNode = this . $element [ 0 ] . parentNode ;
388
- this . container . setScrollSize ( itemsLength * this . itemSize ) ;
408
+
409
+ if ( lengthChanged ) {
410
+ this . container . setScrollSize ( itemsLength * this . itemSize ) ;
411
+ }
412
+
413
+ if ( this . isFirstRender ) {
414
+ this . isFirstRender = false ;
415
+ var startIndex = this . $attrs . mdStartIndex ? this . $scope . $eval ( this . $attrs . mdStartIndex ) : 0 ;
416
+ this . container . scrollTo ( startIndex * this . itemSize ) ;
417
+ }
389
418
390
419
// Detach and pool any blocks that are no longer in the viewport.
391
420
Object . keys ( this . blocks ) . forEach ( function ( blockIndex ) {
0 commit comments