Skip to content

fix(forms): improve select performance - #61949

Closed
atscott wants to merge 2 commits into
angular:mainfrom
atscott:pr-takeover-23784
Closed

atscott wants to merge 2 commits into
angular:mainfrom
atscott:pr-takeover-23784

Conversation

@atscott

@atscott atscott commented Jun 6, 2025

Copy link
Copy Markdown
Contributor

We defer the update until after rendering
is complete for two reasons: first, to avoid repeatedly calling writeValue on every option element until we find the selected one (could be the very last element). Second, to ensure that we perform the write after the DOM elements have been created (this doesn't happen until the end of change detection when animations are enabled).

This is needed to efficiently set the select value when adding/removing options. The previous approach resulted in exponentially more _compareValue calls than the number of option elements (issue #41330).

Finally, this PR fixes an issue with delayed element removal when using the animations module (in all browsers). Previously when a selected option was removed (so no option matched the ngModel anymore), Angular changed the select element value before actually removing the option from the DOM. Then when the option was finally removed from the DOM, the browser would change the select value to that of the first option, even though it didn't match the ngModel (issue #18430). Note that this is still somewhat of an application problem when using ngModel. The model value still needs to be updated to a valid value when the selected value is deleted or it will be out of sync with the DOM.

Fixes #41330, fixes #18430.

TGP
requires an update to the screenshots in one test, with minor shifting in the placement of the text in the select control (see cl/767187673)

Though there is a green TGP, this changes the timing of values being written to the DOM, which still caries some risk. We should merge to main only to give more time for things to be discovered internally prior to external release.

@ngbot ngbot Bot added this to the Backlog milestone Jun 6, 2025
@atscott
atscott force-pushed the pr-takeover-23784 branch 2 times, most recently from 7d1a59b to 3f81874 Compare June 6, 2025 20:12
@atscott atscott added the target: minor This PR is targeted for the next minor release label Jun 6, 2025
We defer the update until after rendering
is complete for two reasons: first, to avoid repeatedly calling
`writeValue` on every option element until we find the selected one
(could be the very last element). Second, to ensure that we perform the
write after the DOM elements have been created (this doesn't happen
until the end of change detection when animations are enabled).

This is needed to efficiently set the select value when adding/removing options. The
previous approach resulted in exponentially more `_compareValue` calls than the number
of option elements (issue angular#41330).

Finally, this PR fixes an issue with delayed element removal when using the animations
module (in all browsers). Previously when a selected option was removed (so no option
matched the ngModel anymore), Angular changed the select element value before actually
removing the option from the DOM. Then when the option was finally removed from the DOM,
the browser would change the select value to that of the first option, even though it
didn't match the ngModel (issue angular#18430). Note that this is still
somewhat of an application problem when using `ngModel`. The model value
still needs to be updated to a valid value when the selected value is
deleted or it will be out of sync with the DOM.

Fixes angular#41330, fixes angular#18430.
@atscott
atscott force-pushed the pr-takeover-23784 branch from 3f81874 to cd961cb Compare June 6, 2025 22:06
@atscott
atscott force-pushed the pr-takeover-23784 branch from 3b278f1 to fb7134d Compare June 6, 2025 22:10
@atscott
atscott marked this pull request as ready for review June 10, 2025 03:31
private _compareWith: (o1: any, o2: any) => boolean = Object.is;
// We need this because we might be in the process of destroying the root
// injector, which is marked as destroyed before running destroy hooks.
// Attempting to use afterNextRender with the node injector would evntually

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"eventually"

@AndrewKushnir AndrewKushnir added action: merge The PR is ready for merge by the caretaker merge: caretaker note Alert the caretaker performing the merge to check the PR for an out of normal action needed or note labels Jun 10, 2025
@AndrewKushnir

Copy link
Copy Markdown
Contributor

This PR was merged into the repository by commit 4f0221e.

The changes were merged into the following branches: main

@angular-automatic-lock-bot

Copy link
Copy Markdown

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot Bot locked and limited conversation to collaborators Jul 11, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

action: merge The PR is ready for merge by the caretaker area: forms merge: caretaker note Alert the caretaker performing the merge to check the PR for an out of normal action needed or note risk: medium target: minor This PR is targeted for the next minor release

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Select dropdowns with ngValue binding perform far worse than expected BrowserAnimationsModule changes the behaviour of select-dropdowns

4 participants