Skip to content

fix(forms): improve select performance and ensure that selected option matches the ngModel value - #23784

Closed
theodorejb wants to merge 1 commit into
angular:mainfrom
theodorejb:select-ie-fix
Closed

theodorejb wants to merge 1 commit into
angular:mainfrom
theodorejb:select-ie-fix

Conversation

@theodorejb

@theodorejb theodorejb commented May 9, 2018 •

Copy link
Copy Markdown
Contributor

Fixes #41330, fixes #14505, fixes #18430.

PR Checklist

PR Type

What kind of change does this PR introduce?

  • Bugfix

What is the current behavior?

Currently Angular calls the writeValue() over and over for every individual option element that is added or removed from the DOM. This results in exponentially more _compareValue calls than the number of option elements (issue #41330).

Furthermore, calling writeValue() when rendering individual option elements causes an issue in Safari and IE 11 where the first option element fails to be deselected when no option matches the select ngModel. This is because Angular sets the select element's value property before appending the option's child text node to the DOM (issue #14505).

Finally, the current behavior causes an issue with delayed element removal when using the animations module (in all browsers). When a selected option is removed (so no option matches the ngModel anymore), Angular changes the select element value before actually removing the option from the DOM. Then when the option is finally removed from the DOM, the browser changes the select value to that of the first option, even though it doesn't match the ngModel (issue #18430).

What is the new behavior?

Rather than calling writeValue() over and over for individual option elements that are added/removed from the DOM, call it once after all the options have finished rendering via the afterNextRender function. This dramatically improves performance and results in correct behavior across all supported browsers.

Does this PR introduce a breaking change?

  • Yes
  • No

@theodorejb theodorejb changed the title fix(forms): deselect first option in select dropdown when it doesn't match select value fix(forms): deselect first option in select dropdown when it doesn't match the ngModel value May 10, 2018
@IgorMinar
IgorMinar requested a review from kara May 11, 2018 14:44
@IgorMinar IgorMinar added area: forms action: review The PR is still awaiting reviews from at least one requested reviewer labels May 11, 2018
@trotyl

trotyl commented Aug 3, 2018

Copy link
Copy Markdown
Contributor

Ref #14507

@theodorejb

Copy link
Copy Markdown
Contributor Author

@trotyl I don't think pull request #14507 was ever finished, since it didn't fix the issue for me when I tested it.

@trotyl

trotyl commented Aug 3, 2018

Copy link
Copy Markdown
Contributor

That's why “ref”, not “duplicate of”.

@JoostK

JoostK commented Aug 3, 2018 •

Copy link
Copy Markdown
Member

I experience the same broken behaviour in Safari 11.0.2

EDIT Updated to 11.1.2 while we're at it, same problem still.

@theodorejb

Copy link
Copy Markdown
Contributor Author

I just tested https://angular-select-ie-bug.stackblitz.io/ in Safari on the latest version of iOS and can confirm that the bug happens there as well.

@theodorejb

theodorejb commented Oct 16, 2018 •

Copy link
Copy Markdown
Contributor Author

@kara This is still an issue in Angular 7.0.0, affecting the latest versions of both IE and Safari. Could you please let me know if there's anything that I need to change, or additional tests I should add before this PR can be merged?

@kriks57

kriks57 commented Nov 7, 2018

Copy link
Copy Markdown

@kara please have a look, and we would also need this fix in angular 4

@jasonaden jasonaden added this to the needsTriage milestone Jan 29, 2019
@tan9

tan9 commented May 24, 2019

Copy link
Copy Markdown

@kara , @jasonaden, @IgorMinar is there anything I can do or @theodorejb can do to make progress on the triage or reviewing this merge request? I am willing to help.

@theodorejb
theodorejb requested a review from a team July 29, 2019 16:41
@dasaweb

dasaweb commented Oct 14, 2019

Copy link
Copy Markdown

@kara , @jasonaden, @IgorMinar is there anything I can do or @theodorejb can do to make progress on the triage or reviewing this merge request? I am willing to help.

This problem has been around for years now, and it's still a big problem for me (and for others, too, see #14505). It would be very helpful if the proposed solution could be incorporated. Thanks a lot!

@theodorejb

Copy link
Copy Markdown
Contributor Author

@AndrewKushnir Can you please review this? It would be really nice if this bug was fixed in Angular 9.

mhevery
mhevery previously approved these changes Oct 17, 2019
@mhevery mhevery added action: merge The PR is ready for merge by the caretaker target: patch This PR is targeted for the next patch release and removed action: review The PR is still awaiting reviews from at least one requested reviewer labels Oct 17, 2019
@mhevery

mhevery commented Oct 17, 2019

Copy link
Copy Markdown
Contributor

presubmit

@AndrewKushnir

AndrewKushnir commented Oct 17, 2019 •

Copy link
Copy Markdown
Contributor

Thanks for the PR @theodorejb. Could you please rebase it on top of the latest master, so CI runs again with the most up-to-date set of tests?

It'd be also helpful if you could provide some additional information regarding the problem:

  • if you tried to explore other solutions to this issue (that potentially would not require setTimeout)
  • if this issue is reproducible with plain HTML and JS, so we can make sure it's a specific browser behavior that is triggering this issue
  • if the problem is reproducible with HTML and JS (i.e. it's not Angular-specific), there might be existing bug reports created for affected browsers. It'd be helpful to have these links, so we can get extra information and extend comments in the code

Thank you.

@theodorejb

theodorejb commented Oct 18, 2019 •

Copy link
Copy Markdown
Contributor Author

Thank you so much for the reply @AndrewKushnir. The PR has been rebased on master now and a couple code comments have been improved.

I spent a bunch of time digging through the select_control_value_accessor.ts code and setting debugger breakpoints to figure out why it behaves the way it does.

When option elements are rendered, Angular calls writeValue on the select component after setting the value of each option element. The problem occurs because writeValue is called for each option before that option's child text node has been added to the DOM. Hence value and selectedIndex are set on the select element before the option has a text node, and when the text node is finally added, Safari and IE ignore the preveously set selectedIndex.

I made a simple JS Bin demonstrating the issue with plain HTML and JavaScript:
https://jsbin.com/vidatokizo/1/edit?html,js,output. In Chrome, Firefox, and Edge no option is selected. In Safari and IE 11 the first option is selected.

The reason that using setTimeout fixes the issue is because it causes selectedIndex to be set after the option child text node has been added, in which case it correctly applies in all browsers.

The only other way to fix the issue would be to somehow ensure that writeValue isn't called on the select component until after the option's text node has been appended. However, I'm not familiar enough with Angular's internals to know how to accomplish this (and it seems like it could require bigger hacks than setTimeout).

This PR is a minimal approach to fix the issue and add tests to ensure it isn't regressed. Later on if someone thinks of another way to solve it the tests will be in place to verify it works.

Please let me know if there's any further information you need to move this forward.

@theodorejb

Copy link
Copy Markdown
Contributor Author

@AndrewKushnir can you review this again? I simplified the code further, moving the call to set the select element's value property inside the timeout and completely removing the need to conditionally set selectedIndex. I also updated one of the tests to add a tick() call like most of the other tests have. That particular test only incidentally worked in Internet Explorer previously without the tick() call since its option elements are already rendered in the DOM with their child text nodes.

@JoostK

JoostK commented Aug 16, 2023

Copy link
Copy Markdown
Member

Some initial thoughts: If the value is updated via set value twice in the same CD cycle, wouldn't the second value get dropped (because an update is already queued)?

That should result in dropping the first update, not the second; flushing the write to the DOM in the afterNextRender hook always reads the current value, not the value that was set when the hook was requested.

@dylhunn dylhunn left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

reviewed-for: fw-forms

I will run a TAP global presubmit tonight. If it's green, we should target 17.0.

@dylhunn

dylhunn commented Sep 19, 2023

Copy link
Copy Markdown
Contributor

Also @theodorejb -- your final commit message is failing lint because it needs a body >20 chars apparently.

@theodorejb

Copy link
Copy Markdown
Contributor Author

@dylhunn Thanks! I rebased now and squashed the last 4 commits with an updated message.

@dylhunn dylhunn added action: review The PR is still awaiting reviews from at least one requested reviewer action: presubmit The PR is in need of a google3 presubmit and removed state: blocked action: discuss action: review The PR is still awaiting reviews from at least one requested reviewer labels Sep 19, 2023
@dylhunn

dylhunn commented Sep 26, 2023

Copy link
Copy Markdown
Contributor

@theodorejb Sorry about this, mind rebasing one more time? My TGP failed to get scheduled (we've been having capacity restrictions...) and I need to try it again.

@theodorejb

Copy link
Copy Markdown
Contributor Author

@dylhunn It's rebased now.

@dylhunn

dylhunn commented Sep 27, 2023 •

Copy link
Copy Markdown
Contributor

TGP is running: google link
Update: TGP results show 22 broken tests across various applications. Mostly ExpressionChangedAfterItHasBeenChecked.

Typical stack trace:

error properties: null({ code: -100, constructor: Function })
Error: NG0100: ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked. Previous value for 'mat-form-field-hide-placeholder': 'false'. Current value: 'true'. Expression location: TaxonomyType component. Find more at [http://go/ngerr/NG0100](https://www.google.com/url?q=http://go/ngerr/NG0100&sa=D)
    at RuntimeError.ZoneAwareError (https://cs.corp.google.com/piper///depot/google3/third_party/javascript/angular2/rc/packages/zone.js/lib/common/error-rewrite.ts?l=121
    at new RuntimeError 
    at throwErrorIfNoChangesMode 
    at bindingUpdated 
    at checkStylingProperty 
    at ɵɵclassProp 
    at MatFormField_HostBindings 
    at processHostBindingOpCodes 
    at refreshView 
    at detectChangesInView 
    at detectChangesInComponent 
    at detectChangesInChildComponents 
    at refreshView 
    at detectChangesInView 
    at detectChangesInEmbeddedViews 

@theodorejb

Copy link
Copy Markdown
Contributor Author

@dylhunn Do the broken tests indicate a problem with this patch? If so, could a similar failing test be added to Angular itself? I'm not sure how I could debug those errors without being able to reproduce them myself.

@AndrewKushnir AndrewKushnir removed their assignment Oct 18, 2023
@atscott atscott self-assigned this Jun 12, 2024
…n matches the ngModel value

Rather than calling `writeValue()` over and over for individual option elements that are
added/removed from the DOM, call it once after all the options have finished rendering
via the `ngAfterViewChecked` hook.

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).

Furthermore, calling `writeValue()` when rendering individual option elements caused
an issue in Safari and IE 11 where the first option element failed to be deselected when
no option matched the select ngModel. This was because Angular set the select element's
value property before appending the option's child text node to the DOM (issue #14505).

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).

Fixes #41330, fixes #14505, fixes #18430.
@AndrewKushnir AndrewKushnir removed the action: presubmit The PR is in need of a google3 presubmit label Jan 11, 2025
@thePunderWoman
thePunderWoman marked this pull request as draft May 21, 2025 15:23
@thePunderWoman
thePunderWoman removed the request for review from AndrewKushnir June 3, 2025 10:56
@thePunderWoman

Copy link
Copy Markdown
Contributor

This would be great to land, but unfortunately the amount of effort involved for us to safely get it submitted is too high. So we're going to close this, but thank you for your contribution. We appreciate you!

@theodorejb

Copy link
Copy Markdown
Contributor Author

Despite the countless hours I put into fixing this, I don't personally care anymore, since I moved my projects to Svelte which has been far easier to contribute to. But I feel bad for all the Angular users who are stuck with these apparently unresolvable bugs.

@atscott

atscott commented Jun 5, 2025

Copy link
Copy Markdown
Contributor

I'm sorry about your experience with this issue. Not all issues have clear fixes and can be done in a backwards compatible way or a way that does not cause other adverse side-effects. The Angular team is committed to bringing existing applications along rather than leaving them behind with breaking changes.

We do have plenty of contributors outside the team who have been able to land changes, including arturovt, ivanwonder, and JeanMeche who has recently been brought on due to his extensive involvement.

@atscott

atscott commented Jun 5, 2025

Copy link
Copy Markdown
Contributor

The history here is also quite long, with extensive involvement from the team over the years. I cannot see each iteration of the change, but concerns with the changes cannot be invalidated. We also have to balance priorities and cannot dedicate infinite amounts of time to any one issue.

I gave this another look after it was closed yesterday and verified real issues with the current iteration of the changes in the internal test failures. I hesitate to even mention this based due to the history here: I think I have addressed those issues and am truly hopeful we can finally resolve this, but I cannot make any promises.

@theodorejb

Copy link
Copy Markdown
Contributor Author

@atscott Thanks for finally getting this merged via #61949!

@theodorejb
theodorejb deleted the select-ie-fix branch June 11, 2025 15:59
@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 12, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

area: core Issues related to the framework runtime area: dev-infra Issues related to Angular's own dev infra (build, test, CI, releasing) area: forms browser: safari cla: yes forms: DOM target: major This PR is targeted for the next major release type: bug/fix

Projects

None yet