angular/components · error · Error

cdk-virtual-scroll: scrollToIndex is currently not supported

Error message

cdk-virtual-scroll: scrollToIndex is currently not supported for the autosize scroll strategy

What it means

AutoSizeVirtualScrollStrategy also does not implement scrollToIndex; calling it throws this error in dev mode. The method is required by the VirtualScrollStrategy interface but remains a TODO in the experimental autosize strategy. Production builds silently do nothing.

Source

Thrown at src/cdk-experimental/scrolling/auto-size-virtual-scroll.ts:171

  /** @docs-private Implemented as part of VirtualScrollStrategy. */
  onContentRendered() {
    if (this._viewport) {
      this._checkRenderedContentSize();
    }
  }

  /** @docs-private Implemented as part of VirtualScrollStrategy. */
  onRenderedOffsetChanged() {
    if (this._viewport) {
      this._checkRenderedContentOffset();
    }
  }

  /** Scroll to the offset for the given index. */
  scrollToIndex(): void {
    if (typeof ngDevMode === 'undefined' || ngDevMode) {
      // TODO(mmalerba): Implement.
      throw Error(
        'cdk-virtual-scroll: scrollToIndex is currently not supported for the autosize' +
          ' scroll strategy',
      );
    }
  }

  /**
   * Update the buffer parameters.
   * @param minBufferPx The minimum amount of buffer rendered beyond the viewport (in pixels).
   * @param maxBufferPx The number of buffer items to render beyond the edge of the viewport (in
   *     pixels).
   */
  updateBufferSize(minBufferPx: number, maxBufferPx: number) {
    if (maxBufferPx < minBufferPx) {
      throw Error('CDK virtual scroll: maxBufferPx must be greater than or equal to minBufferPx');
    }
    this._minBufferPx = minBufferPx;
    this._maxBufferPx = maxBufferPx;

View on GitHub (pinned to 0411926e7d)

Solutions

  1. Scroll the viewport's native element manually: viewport.elementRef.nativeElement.scrollTop = offset, using an offset you track yourself.
  2. Switch to a strategy that supports scrollToIndex (fixed-size itemSize or the non-experimental variable-size strategy).
  3. Avoid programmatic index-based scrolling with autosize until the TODO is implemented upstream.

Example fix

// before
viewport.scrollToIndex(targetIndex); // throws with autosize
// after
viewport.elementRef.nativeElement.scrollTop = savedPixelOffset;
Defensive patterns

Strategy: validation

Validate before calling

if (viewport.strategy instanceof AutoSizeVirtualScrollStrategy) {
  // scrollToIndex unsupported; set scrollTop manually instead
  viewport.elementRef.nativeElement.scrollTop = targetOffset;
} else {
  viewport.scrollToIndex(targetIndex);
}

Try / catch

try {
  viewport.scrollToIndex(i);
} catch {
  viewport.elementRef.nativeElement.scrollTop = savedOffsetForIndex(i);
}

Prevention

When it happens

Trigger: Calling CdkVirtualScrollViewport.scrollToIndex() (or scrollToOffset, which delegates per strategy) when the viewport uses AutoSizeVirtualScrollStrategy in a development build.

Common situations: Programmatic scrolling: jumping to a saved scroll position, scroll-to-top buttons, navigating back to a list and restoring position; code that worked with fixed-size strategy moved to autosize.

Related errors


AI-assisted analysis of angular/components@0411926e7d (2026-08-31). Data as JSON: /api/errors/f70531f60a234ec1. Report an issue: GitHub.