6
6
* found in the LICENSE file at https://angular.io/license
7
7
*/
8
8
9
- import { Injectable , OnDestroy , Self , NgZone } from '@angular/core' ;
9
+ import { Injectable , OnDestroy , Self , afterNextRender , inject , Injector } from '@angular/core' ;
10
10
import { ControlContainer } from '@angular/forms' ;
11
11
import { Observable , Subject } from 'rxjs' ;
12
- import { take } from 'rxjs/operators' ;
13
12
14
13
import { EditEventDispatcher } from './edit-event-dispatcher' ;
15
14
@@ -30,10 +29,11 @@ export class EditRef<FormValue> implements OnDestroy {
30
29
/** The value to set the form back to on revert. */
31
30
private _revertFormValue : FormValue ;
32
31
32
+ private _injector = inject ( Injector ) ;
33
+
33
34
constructor (
34
35
@Self ( ) private readonly _form : ControlContainer ,
35
36
private readonly _editEventDispatcher : EditEventDispatcher < EditRef < FormValue > > ,
36
- private readonly _ngZone : NgZone ,
37
37
) {
38
38
this . _editEventDispatcher . setActiveEditRef ( this ) ;
39
39
}
@@ -44,14 +44,17 @@ export class EditRef<FormValue> implements OnDestroy {
44
44
* applicable.
45
45
*/
46
46
init ( previousFormValue : FormValue | undefined ) : void {
47
- // Wait for the zone to stabilize before caching the initial value.
47
+ // Wait for the next render before caching the initial value.
48
48
// This ensures that all form controls have been initialized.
49
- this . _ngZone . onStable . pipe ( take ( 1 ) ) . subscribe ( ( ) => {
50
- this . updateRevertValue ( ) ;
51
- if ( previousFormValue ) {
52
- this . reset ( previousFormValue ) ;
53
- }
54
- } ) ;
49
+ afterNextRender (
50
+ ( ) => {
51
+ this . updateRevertValue ( ) ;
52
+ if ( previousFormValue ) {
53
+ this . reset ( previousFormValue ) ;
54
+ }
55
+ } ,
56
+ { injector : this . _injector } ,
57
+ ) ;
55
58
}
56
59
57
60
ngOnDestroy ( ) : void {
0 commit comments