Skip to content

Support an optional view identity key on @if branches #72095

Open

Description

@EthanSK

Which @angular/* package(s) are relevant/related to the feature request?

compiler, core

Description

A clip editor has one selected clip, or none. While editing clip A, a user types a draft caption into a native input. If A's title changes, the input should keep its draft. If the user selects B, the new editor should start empty. Both selections satisfy the same @if, so Angular updates the bindings on the existing element and B inherits A's draft.

The same distinction comes up in a media preview. A one-shot GIF can keep its image element when its loading indicator goes away. Selecting a new playback session can give the entire preview fresh elements and fresh component-local state, even if the two sessions use the same asset URL. If two images need to coexist while one preloads, that's a collection and a tracked @for still fits.

Proposed solution

Allow a conditional branch to declare what identifies its current view. One possible spelling:

@if (selected(); as clip; track clip.id) {
  <input [attr.aria-label]="clip.title">
}
  • While the branch is active and the key stays equal, update its bindings and keep its DOM nodes and child component instances. This includes replacing the model object with a new object carrying the same ID.
  • When that branch's key changes, destroy its old view (including child teardown) and create a fresh one in the same render. Returning from B to A creates a new A view. A changed key intentionally discards local state such as focus or an unsaved draft, so the key should be stable for the life of the selection.
  • Evaluate the key only for the selected branch, with the condition alias in scope. Leaving the branch destroys its view as usual, including when the condition becomes falsy. An unkeyed branch keeps today's behavior.

A design choice is whether the key should always compare the last and current evaluated values, including after an in-place object mutation. That would make a key a deliberate reset signal and would differ from @for's documented same-reference behavior.

Alternatives considered

With immutable clip records, this works today:

@let clip = selected();
@for (item of clip ? [clip] : []; track item.id) {
  <input [attr.aria-label]="item.title">
}

The loop and temporary collection communicate “list” where the data model says “one optional selection.” Resetting the input's value explicitly also works for this example. It gets harder when the branch contains several stateful elements or child components. A structural directive could manage the same embedded-view lifetime, at the cost of adding an app-level directive for a template operation.

Related: #51302 proposed reloading a child component. This request puts the key on the parent's conditional branch, which can contain native elements and several children.

Reproduction on Angular 22.3.0

A standalone Angular TestBed example compares actual DOM node identity and native input values. All four tests passed in a fresh Angular CLI project (CLI 22.2.8, Vitest 5.1.1, jsdom). The compiler also reports Unrecognized conditional parameter "track clip.id" for the proposed spelling.

Runnable spec

Save as src/app/view-identity.spec.ts in a standard Angular 22 CLI app with Vitest, then run npx ng test --watch=false --include src/app/view-identity.spec.ts.

import { Component, signal } from '@angular/core';
import { TestBed } from '@angular/core/testing';
import { parseTemplate } from '@angular/compiler';
import { describe, expect, it } from 'vitest';

interface Clip {
  id: string;
  title: string;
}

@Component({
  standalone: true,
  template: `
    @let clip = selected();
    @if (clip) {
      <input class="plain" [attr.aria-label]="clip.title">
    }
    @for (item of clip ? [clip] : []; track item.id) {
      <input class="keyed" [attr.aria-label]="item.title">
    }
  `,
})
class ClipEditor {
  readonly selected = signal<Clip | null>({ id: 'A', title: 'First clip' });
}

function mountEditor() {
  const fixture = TestBed.createComponent(ClipEditor);
  const host: HTMLElement = fixture.nativeElement;
  fixture.detectChanges();

  function input(kind: 'plain' | 'keyed') {
    const node = host.querySelector<HTMLInputElement>('input.' + kind);
    if (!node) throw new Error('Expected the ' + kind + ' input to be mounted');
    return node;
  }

  function select(clip: Clip | null) {
    fixture.componentInstance.selected.set(clip);
    fixture.detectChanges();
  }

  return { host, input, select };
}

describe('one selected clip', () => {
  it('updates bindings but keeps both inputs and their drafts for the same ID', () => {
    const { input, select } = mountEditor();
    const plain = input('plain');
    const keyed = input('keyed');
    plain.value = 'Draft A';
    keyed.value = 'Draft A';

    select({ id: 'A', title: 'Updated title' });

    expect(input('plain')).toBe(plain);
    expect(input('keyed')).toBe(keyed);
    for (const node of [plain, keyed]) {
      expect(node.getAttribute('aria-label')).toBe('Updated title');
      expect(node.value).toBe('Draft A');
    }
  });

  it('keeps the plain input but replaces the tracked input when the ID changes', () => {
    const { input, select } = mountEditor();
    const plain = input('plain');
    const keyed = input('keyed');
    plain.value = 'Draft A';
    keyed.value = 'Draft A';

    select({ id: 'B', title: 'Second clip' });

    expect(input('plain')).toBe(plain);
    expect(plain.getAttribute('aria-label')).toBe('Second clip');
    expect(plain.value).toBe('Draft A');
    expect(input('keyed')).not.toBe(keyed);
    expect(input('keyed').getAttribute('aria-label')).toBe('Second clip');
    expect(input('keyed').value).toBe('');
  });

  it('removes both inputs on clear and creates fresh ones when the same clip returns', () => {
    const { host, input, select } = mountEditor();
    const plain = input('plain');
    const keyed = input('keyed');

    select(null);
    expect(host.querySelectorAll('input')).toHaveLength(0);

    select({ id: 'A', title: 'First clip' });
    expect(input('plain')).not.toBe(plain);
    expect(input('keyed')).not.toBe(keyed);
  });

  it('reports the proposed track clause as an unsupported conditional parameter', () => {
    const template = '@if (selected(); as clip; track clip.id) { <input> }';
    const { errors } = parseTemplate(template, 'example.html');
    expect(errors?.map(error => error.msg)).toContain('Unrecognized conditional parameter "track clip.id"');
  });
});

Activity

  1. added
    area: core
    on Nov 18, 2026
  2. added this to the needsTriage milestone on Nov 18, 2026
  3. Flarette commented on Nov 20, 2026

    @Flarette
    Contributor

    Related: #72140
    If it lands this behaviour would be possible through the @boundary(track clipId()) API

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: core

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions