Title: Support an optional view identity key on @if branches · Issue #72095 · angular/angular · GitHub
Open Graph Title: Support an optional view identity key on @if branches · Issue #72095 · angular/angular
X Title: Support an optional view identity key on @if branches · Issue #72095 · angular/angular
Description: 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 change...
Open Graph Description: 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 ...
X Description: 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 ...
Opengraph URL: https://github.com/angular/angular/issues/72095
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Support an optional view identity key on @if branches","articleBody":"### Which @angular/* package(s) are relevant/related to the feature request?\n\ncompiler, core\n\n### Description\n\nA 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.\n\nThe 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.\n\n### Proposed solution\n\nAllow a conditional branch to declare what identifies its current view. One possible spelling:\n\n```html\n@if (selected(); as clip; track clip.id) {\n \u003cinput [attr.aria-label]=\"clip.title\">\n}\n```\n\n- 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.\n- 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.\n- 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.\n\nA 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](https://angular.dev/guide/templates/control-flow#why-is-track-in-for-blocks-important).\n\n### Alternatives considered\n\nWith immutable clip records, this works today:\n\n```html\n@let clip = selected();\n@for (item of clip ? [clip] : []; track item.id) {\n \u003cinput [attr.aria-label]=\"item.title\">\n}\n```\n\nThe 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.\n\nRelated: #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.\n\n### Reproduction on Angular 22.3.0\n\nA 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.\n\n\u003cdetails>\n\u003csummary>Runnable spec\u003c/summary>\n\nSave 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`.\n\n```ts\nimport { Component, signal } from '@angular/core';\nimport { TestBed } from '@angular/core/testing';\nimport { parseTemplate } from '@angular/compiler';\nimport { describe, expect, it } from 'vitest';\n\ninterface Clip {\n id: string;\n title: string;\n}\n\n@Component({\n standalone: true,\n template: `\n @let clip = selected();\n @if (clip) {\n \u003cinput class=\"plain\" [attr.aria-label]=\"clip.title\">\n }\n @for (item of clip ? [clip] : []; track item.id) {\n \u003cinput class=\"keyed\" [attr.aria-label]=\"item.title\">\n }\n `,\n})\nclass ClipEditor {\n readonly selected = signal\u003cClip | null>({ id: 'A', title: 'First clip' });\n}\n\nfunction mountEditor() {\n const fixture = TestBed.createComponent(ClipEditor);\n const host: HTMLElement = fixture.nativeElement;\n fixture.detectChanges();\n\n function input(kind: 'plain' | 'keyed') {\n const node = host.querySelector\u003cHTMLInputElement>('input.' + kind);\n if (!node) throw new Error('Expected the ' + kind + ' input to be mounted');\n return node;\n }\n\n function select(clip: Clip | null) {\n fixture.componentInstance.selected.set(clip);\n fixture.detectChanges();\n }\n\n return { host, input, select };\n}\n\ndescribe('one selected clip', () => {\n it('updates bindings but keeps both inputs and their drafts for the same ID', () => {\n const { input, select } = mountEditor();\n const plain = input('plain');\n const keyed = input('keyed');\n plain.value = 'Draft A';\n keyed.value = 'Draft A';\n\n select({ id: 'A', title: 'Updated title' });\n\n expect(input('plain')).toBe(plain);\n expect(input('keyed')).toBe(keyed);\n for (const node of [plain, keyed]) {\n expect(node.getAttribute('aria-label')).toBe('Updated title');\n expect(node.value).toBe('Draft A');\n }\n });\n\n it('keeps the plain input but replaces the tracked input when the ID changes', () => {\n const { input, select } = mountEditor();\n const plain = input('plain');\n const keyed = input('keyed');\n plain.value = 'Draft A';\n keyed.value = 'Draft A';\n\n select({ id: 'B', title: 'Second clip' });\n\n expect(input('plain')).toBe(plain);\n expect(plain.getAttribute('aria-label')).toBe('Second clip');\n expect(plain.value).toBe('Draft A');\n expect(input('keyed')).not.toBe(keyed);\n expect(input('keyed').getAttribute('aria-label')).toBe('Second clip');\n expect(input('keyed').value).toBe('');\n });\n\n it('removes both inputs on clear and creates fresh ones when the same clip returns', () => {\n const { host, input, select } = mountEditor();\n const plain = input('plain');\n const keyed = input('keyed');\n\n select(null);\n expect(host.querySelectorAll('input')).toHaveLength(0);\n\n select({ id: 'A', title: 'First clip' });\n expect(input('plain')).not.toBe(plain);\n expect(input('keyed')).not.toBe(keyed);\n });\n\n it('reports the proposed track clause as an unsupported conditional parameter', () => {\n const template = '@if (selected(); as clip; track clip.id) { \u003cinput> }';\n const { errors } = parseTemplate(template, 'example.html');\n expect(errors?.map(error => error.msg)).toContain('Unrecognized conditional parameter \"track clip.id\"');\n });\n});\n```\n\n\u003c/details>\n","author":{"url":"https://github.com/EthanSK","@type":"Person","name":"EthanSK"},"datePublished":"2026-11-18T22:29:33Z","url":"https://github.com/angular/angular/issues/72095"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:4dd9f309-6232-1838-dc41-1bd3f18c894c |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 9578:1FE434:FFEE55:152443D:6B01DB7F |
| html-safe-nonce | c5f856229b16cff2b0f09e10856b929439d43d44b642eab3f6205696ac8bc51c |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5NTc4OjFGRTQzNDpGRkVFNTU6MTUyNDQzRDo2QjAxREI3RiIsInZpc2l0b3JfaWQiOiIxNzQ3MTExNTU3Mjg1OTI3ODA3IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | a4e09f5ef25bea797bc7f5afd29c4883cc62e7f5596cf6506f5a90f57799cdce |
| hovercard-subject-tag | issue:6478032947 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/angular/angular/72095/issue_layout |
| twitter:image | https://opengraph.githubassets.com/598af413671eccc7a60ea41295cc62fdeeaf6db65ab1ca2700658a4d7fac3565/angular/angular/issues/72095 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/598af413671eccc7a60ea41295cc62fdeeaf6db65ab1ca2700658a4d7fac3565/angular/angular/issues/72095 |
| og:image:alt | 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 ... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | EthanSK |
| hostname | github.com |
| expected-hostname | github.com |
| None | cc5dfdd32f62ebd3223cab3af3aa2b268d3605218237326a48a99d1efd89495c |
| turbo-cache-control | no-preview |
| go-import | github.com/angular/angular git https://github.com/angular/angular.git |
| octolytics-dimension-user_id | 139426 |
| octolytics-dimension-user_login | angular |
| octolytics-dimension-repository_id | 24195339 |
| octolytics-dimension-repository_nwo | angular/angular |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 24195339 |
| octolytics-dimension-repository_network_root_nwo | angular/angular |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 8c77c9e8439031ad54c75a250694842892f693e1 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width