@@ -365,6 +365,12 @@ function VirtualRepeatController($scope, $element, $attrs, $browser, $document,
365
365
/** @type {number } Most recently seen length of items. */
366
366
this . itemsLength = 0 ;
367
367
368
+ /**
369
+ * @type {!Function } Unwatch callback for item size (when md-items-size is
370
+ * not specified), or angular.noop otherwise.
371
+ */
372
+ this . unwatchItemSize_ = angular . noop ;
373
+
368
374
/**
369
375
* Presently rendered blocks by repeat index.
370
376
* @type {Object<number, !VirtualRepeatController.Block }
@@ -407,6 +413,11 @@ VirtualRepeatController.prototype.link_ =
407
413
408
414
/** @private Attempts to set itemSize by measuring a repeated element in the dom */
409
415
VirtualRepeatController . prototype . readItemSize_ = function ( ) {
416
+ if ( this . itemSize ) {
417
+ // itemSize was successfully read in a different asynchronous call.
418
+ return ;
419
+ }
420
+
410
421
this . items = this . repeatListExpression ( this . $scope ) ;
411
422
this . parentNode = this . $element [ 0 ] . parentNode ;
412
423
var block = this . getBlock_ ( 0 ) ;
@@ -430,14 +441,22 @@ VirtualRepeatController.prototype.readItemSize_ = function() {
430
441
VirtualRepeatController . prototype . containerUpdated = function ( ) {
431
442
// If itemSize is unknown, attempt to measure it.
432
443
if ( ! this . itemSize ) {
433
- this . $$rAF ( angular . bind ( this , this . readItemSize_ ) ) ;
444
+ this . unwatchItemSize_ = this . $scope . $watch (
445
+ this . repeatListExpression ,
446
+ angular . bind ( this , function ( items ) {
447
+ if ( items && items . length ) {
448
+ this . $$rAF ( angular . bind ( this , this . readItemSize_ ) ) ;
449
+ }
450
+ } ) ) ;
451
+ this . $scope . $digest ( ) ;
434
452
435
453
return ;
436
454
} else if ( ! this . sized ) {
437
455
this . items = this . repeatListExpression ( this . $scope ) ;
438
456
}
439
457
440
458
if ( ! this . sized ) {
459
+ this . unwatchItemSize_ ( ) ;
441
460
this . sized = true ;
442
461
this . $scope . $watchCollection ( this . repeatListExpression ,
443
462
angular . bind ( this , this . virtualRepeatUpdate_ ) ) ;
0 commit comments