Skip to content

KeyValuePipe keeps the previous Map order when only the insertion order changes #72114

Open

Description

@nagahara-naoki

Hi Angular team 👋

I ran into a small surprise with keyvalue: null. I reversed the entries in a Map and passed the new map to my template. A plain @for picked up the new order, but the row using the pipe stayed in the old order.

How to reproduce

Paste this into src/main.ts in an Angular CLI app, run ng serve, and click Reverse once.

import {KeyValuePipe} from '@angular/common';
import {Component, signal} from '@angular/core';
import {bootstrapApplication} from '@angular/platform-browser';

@Component({
  selector: 'app-root',
  imports: [KeyValuePipe],
  template: `
    <p>Direct: @for (pair of rows(); track pair[0]) { <span>{{ pair[0] }}</span> }</p>
    <p>Pipe: @for (pair of rows() | keyvalue: null; track pair.key) { <span>{{ pair.key }}</span> }</p>
    <button (click)="reverse()">Reverse</button>
  `,
})
class App {
  readonly rows = signal(new Map([['b', 2], ['a', 1], ['c', 3]]));

  reverse() {
    this.rows.update(rows => new Map([...rows].reverse()));
  }
}

bootstrapApplication(App);
Before the click After the click
Direct iteration b a c c a b
keyvalue: null b a c b a c

I expected both rows to show c a b, since passing null tells the pipe to use the input’s natural order. If I then change the value of just one entry, the pipe catches up. KeyValuePipe docs

I also tried calling transform() directly. A fresh pipe gives me c a b for the reversed map. A pipe that already processed the original map gives me b a c for that same reversed map.

I found #71893 while looking into this. That example switches the comparator to null. Here it’s null from the first render, and only the insertion order changes.

Tested with Angular and Angular CLI 22.3.0 (@angular/common, AOT), on macOS with Chrome 157.0.8103.79.

Activity

  1. added this to the needsTriage milestone on Nov 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: commonIssues related to APIs in the @angular/common package

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions