8000 Reactive form reinitialization breaks directive valueChanges subscription without triggering ngOnChanges · Issue #62593 · angular/angular · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
Reactive form reinitialization breaks directive valueChanges subscription without triggering ngOnChanges #62593
Closed as not planned
@vikenpateltyler

Description

@vikenpateltyler

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

forms

Is this a regression?

No

Description

When a directive is attached to an input element that uses formControlName, and the underlying form is re-initialized (by assigning a new FormGroup), the directive loses its valueChanges subscription.

However, Angular does not trigger ngOnChanges() on the directive even though the FormControl instance has changed due to the new form. This leads to stale or broken subscriptions within the directive unless workarounds like ngDoCheck() or manual re-binding are implemented.

Steps to Reproduce:
Create a reactive form and bind an input with formControlName.

Attach a custom directive to the input that subscribes to control.valueChanges in ngOnInit() or ngOnChanges().

Reinitialize the form (e.g. via a button that creates a new FormGroup instance).

Please provide a link to a minimal reproduction of the bug

https://stackblitz.com/edit/angular-ce8wsnhy?file=src%2Fapp%2Fapp.component.ts

Please provide the exception or error you saw

**Observe:** the directive is not notified of the new FormControl, and its subscription no longer works.


basically the issue is , once form gets re initialized, directive not detecting change automatically.

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

Angular Version: 18

Anything else?

Expected Behavior:
The directive should receive ngOnChanges() when the form is re-bound so that it can re-subscribe to the new FormControl's valueChanges.

Workaround:
Using ngDoCheck() and manually comparing NgControl.control instances works, but feels like a hack. Explicit [formControl]="..." + [directiveInput]="..." also works but requires a template change.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0