Skip to content

formControl.setValue inside a reactive context causes tracking of all signals read by value/validity change subscribers #67073

Closed

Description

@Arigatouz

Which @angular/* package(s) are the source of the bug?

core

Is this a regression?

Yes

Description

After upgrading from Angular 19 to Angular 21

A component that uses AfterRenderEffect started behaving unexpectedly. When the user clicks a button in a toggle group, the selected value should be reflected correctly, but instead the view is reset to the main title after a render cycle. This appears to be triggered when a form control’ssetValue('main') is called, which causes the render to run in a way that overrides the user’s selection.
If I comment out the setValue('main') call, the component behaves as expected and the selection is preserved, with no unexpected re-renders.
well i susbect that (iam not sure) setValue() is now interacting differently with change detection and AfterRenderEffect between Angular 19 and Angular 21. I am trying to understand whether this is an intentional change in how setValue emits events or triggers rendering, or a regression in how it collaborates with render effects.

Link v19, where it was working link

link v21, where it was broke link

Please provide a link to a minimal reproduction of the bug

https://stackblitz.com/edit/stackblitz-starters-tkg2lk9c?file=package-lock.json,src%2Fmain.ts

Please provide the exception or error you saw


Please provide the environment you discovered this bug in (run ng version)

Angular CLI       : 21.0.2
Angular           : 21.0.3
Node.js           : 20.19.1
Package Manager   : npm 10.8.2
Operating System  : linux x64

┌───────────────────────────┬───────────────────┬───────────────────┐
│ Package                   │ Installed Version │ Requested Version │
├───────────────────────────┼───────────────────┼───────────────────┤
│ @angular/animations       │ 21.0.3            │ ^21.0.3           │
│ @angular/build            │ 21.0.2            │ ^21.0.2           │
│ @angular/cdk              │ 21.1.4            │ ^21.1.4           │
│ @angular/cli              │ 21.0.2            │ ^21.0.2           │
│ @angular/common           │ 21.0.3            │ ^21.0.3           │
│ @angular/compiler         │ 21.0.3            │ ^21.0.3           │
│ @angular/compiler-cli     │ 21.0.3            │ ^21.0.3           │
│ @angular/core             │ 21.0.3            │ ^21.0.3           │
│ @angular/forms            │ 21.0.3            │ ^21.0.3           │
│ @angular/material         │ 21.1.4            │ ^21.1.4           │
│ @angular/platform-browser │ 21.0.3            │ ^21.0.3           │
│ @angular/router           │ 21.0.3            │ ^21.0.3           │
│ rxjs                      │ 7.8.2             │ ^7.8.1            │
│ typescript                │ 5.9.3             │ ^5.9.3            │
└───────────────────────────┴───────────────────┴───────────────────┘

Anything else?

No response

Activity

  1. SkyZeroZx commented on Feb 14, 2026

    @SkyZeroZx
    Contributor

    I've been testing by switching between Angular versions.

    I can reproduce the same problem with Angular 20.0.0.
    From what I've observed, it doesn't happen in Angular 19.1.0 or 19.2.0. So, at some point between 19.2.X and 20.0.0, some change was introduced.

  2. atscott commented on Feb 14, 2026

    @atscott
    Contributor

    Effects and afterRender were not stable until v20 and their scheduling did change. Closing, as there’s nothing here to indicate there’s a bug in the framework, but that there was a breaking change to scheduling for an api not marked as stable (there was).

  3. atscott commented on Feb 14, 2026

    @atscott
    Contributor

    reopening for further investigation. At a glance, it's unclear why setValue even tracks any signals

  4. atscott commented on Feb 14, 2026

    @atscott
    Contributor

    In the meantime, you can address this by running the setValue inside untracked. The implementation of forms should do this inside setValue. Likely something is getting tracked by the value update emission, causing subscribers to read some signal somewhere.

    Edit: in this case, it's the tabIndex of MatButtonToggle

  5. added this to the needsTriage milestone on Feb 14, 2026
  6. changed the title [-]Strange Behavior of AfterRenderEffect, with setValue() inside[/-] [+]`formControl.setValue` inside a reactive context causes tracking of all signals read by value/validity change subscribers[/+] on Feb 14, 2026
  7. JeanMeche commented on Feb 14, 2026

    @JeanMeche
    Member

    There is an underlying signal with _index on the MatButtonToggleGroup that gets read.

  8. added 2 commits that reference this issue on Feb 15, 2026
    3294b79
    40dcad6
  9. imaksp commented on Feb 16, 2026

    @imaksp
    Contributor

    Explicit effect as suggested in #56155 would prevent such issues, currently we need to add untracked to prevent such unintended tracking.

  10. JeanMeche commented on Feb 16, 2026

    @JeanMeche
    Member

    We should be able to make setValue/patchValue untracked. This is what we're looking at with #67077.

  11. imaksp commented on Feb 16, 2026

    @imaksp
    Contributor

    Yes, I have checked that, but this can happen in user code too, most of time we have only 1 or 2 dependencies in effect, so automatic dynamic tracking is not needed. sometime we only need to watch specific signals without worrying about other signals calls inside a function.
    we can achieve this behaviour with using current effect and untracked but still Angular should consider adding new option or function for it.

  12. alxhub commented on Feb 18, 2026

    @alxhub
    Member

    Untracking within setValue itself is the right answer here (#67077)

  13. added a commit that references this issue on Feb 19, 2026
    2061fd8
  14. angular-automatic-lock-bot commented on Mar 22, 2026

    @angular-automatic-lock-bot

    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.

  15. locked and limited conversation to collaborators on Mar 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions