Repository navigation
Description
🐞 bug report
Affected Package
The issue seems to be related to package @angular/platform-browserIs this a regression?
Yes, the previous version in which this bug was not present was: v8.* (8.2.14)Description
Upgrading to Angular v9 broke the functionality of <select> for us. The *ngFor directive we use for populating it with <option> elements now all of the sudden re-renders items if you simply refresh the collection with new instances, even if their contents are the same. This resets the selected option, which is not expected and not desirable. This wasn't happening in v8. This isn't happening in v9 either, unless BrowserAnimationsModule is imported by the app-module. This also can be worked around by using trackBy, but for that you'd need to track down all the problematic ngFor directives after upgrading to v9.
🔬 Minimal Reproduction
Click here for v9 repro repository
Click here to see the same v9 repro app in action
Same demo working without problems in Angular v8 - although StackBlitz doesn't have this problem with v9 either (not sure why), but at least helps illustrate how it was working before
The v9 repro is based on ng new of Angular CLI 9.0.5. If building yourself, run locally with ng serve, because if imported into StackBlitz, the issue can't be reproduced.
- Choose an item in any drop down (the two are linked and simply illustrate the
trackBydifference) - Notice that the first button adds a new item but the selected option doesn't get reset.
- Clicking the second button though resets the selected option in the first drop down.
🔥 Exception or Error
None
🌍 Your Environment
Angular Version:
Angular CLI: 9.0.5
Node: 10.16.3
OS: win32 x64
Angular: 9.0.5
... animations, cli, common, compiler, compiler-cli, core, forms
... language-service, platform-browser, platform-browser-dynamic
... router
Ivy Workspace: Yes
Package Version
-----------------------------------------------------------
@angular-devkit/architect 0.900.5
@angular-devkit/build-angular 0.900.5
@angular-devkit/build-optimizer 0.900.5
@angular-devkit/build-webpack 0.900.5
@angular-devkit/core 9.0.5
@angular-devkit/schematics 9.0.5
@ngtools/webpack 9.0.5
@schematics/angular 9.0.5
@schematics/update 0.900.5
rxjs 6.5.4
typescript 3.7.5
webpack 4.41.2
Anything else relevant?
Happens only in Angular v9, when BrowserAnimationsModule is imported into AppModule and ngFor is not set up with trackBy.
Activity
C0ZEN commentedon Mar 6, 2020
I could not tell you if this is really a bug or a feature of the v9 nevertheless the
trackByfeature is dedicated to avoid re-rendering the DOM to improve the performances.It seems to me that you should have used a
trackByand your issue is in fact, a bad design from your project.Even if Angular considers it as a bug, you should really use the
trackByanytime it is possible.xonuzofa commentedon Apr 28, 2020
This is still happening on Angular 9.1.3. Without using
trackBy, when assigning a new list to the*ngForlist in a select, it sets the select back to the default value '', but the form contains the correct id and the formControlName is not working with select correctly.Also figured out
BrowserAnimationsModuleis the cause of this when adding to AppModuleatscott commentedon May 8, 2020
This appears to be related to the breaking change related to insertion order (reference):
In the stackblitz example, you can see that when Ivy is disabled, the new items are added to the front of the list (add a break on: subtree modifications to the dom). If you go to the settings in stackblitz and check the box to "Enable Ivy", you'll see that new items are added to the end of the list. To clarify, both Ivy and View Engine are re-rendering the items because you don't have
trackBy. The difference is the location of the newly inserted items.Because
BrowserAnimationsModulequeues up the removal of the old items in order to animate them, you will briefly have duplicate items in the list if you don't usetrackBy. In the Ivy example, since the old items being removed are above the new items, when they're removed, the browser sees this as the selected item being removed.As a quick fix, I would recommend using
trackByto avoid this issue.37 remaining items
petebacondarwin commentedon May 24, 2021
Having discussed offline with @atscott - I now understand that this is part of how Angular works (by design) and it just happens that the browser didn't notice with View Engine, while it becomes obvious under Ivy.
Therefore this specific case needs to be listed more clearly in our Ivy breaking changes so that people can more quickly resolve their problem.
docs: add a note about <select> and nested <option>s in Ivy compat guide
docs: add a note about <select> and nested <option>s in Ivy compat gu…
docs: add a note about <select> and nested <option>s in Ivy compat gu…
docs: add a note about <select> and nested <option>s in Ivy compat gu…
angular-automatic-lock-bot commentedon Jun 27, 2021
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.