9
9
import { Directionality } from '@angular/cdk/bidi' ;
10
10
import { DOCUMENT } from '@angular/common' ;
11
11
import {
12
- AfterViewInit ,
13
12
Directive ,
14
13
ElementRef ,
15
14
EventEmitter ,
@@ -27,6 +26,10 @@ import {
27
26
Self ,
28
27
InjectionToken ,
29
28
booleanAttribute ,
29
+ afterNextRender ,
30
+ AfterViewInit ,
31
+ inject ,
32
+ Injector ,
30
33
} from '@angular/core' ;
31
34
import { coerceElement , coerceNumberProperty } from '@angular/cdk/coercion' ;
32
35
import { BehaviorSubject , Observable , Observer , Subject , merge } from 'rxjs' ;
@@ -207,6 +210,8 @@ export class CdkDrag<T = any> implements AfterViewInit, OnChanges, OnDestroy {
207
210
} ,
208
211
) ;
209
212
213
+ private _injector = inject ( Injector ) ;
214
+
210
215
constructor (
211
216
/** Element that the draggable is attached to. */
212
217
public element : ElementRef < HTMLElement > ,
@@ -296,35 +301,35 @@ export class CdkDrag<T = any> implements AfterViewInit, OnChanges, OnDestroy {
296
301
}
297
302
298
303
ngAfterViewInit ( ) {
299
- // Normally this isn't in the zone, but it can cause major performance regressions for apps
300
- // using `zone-patch-rxjs` because it'll trigger a change detection when it unsubscribes.
301
- this . _ngZone . runOutsideAngular ( ( ) => {
302
- // We need to wait for the zone to stabilize, in order for the reference
303
- // element to be in the proper place in the DOM. This is mostly relevant
304
- // for draggable elements inside portals since they get stamped out in
305
- // their original DOM position and then they get transferred to the portal.
306
- this . _ngZone . onStable . pipe ( take ( 1 ) , takeUntil ( this . _destroyed ) ) . subscribe ( ( ) => {
304
+ // We need to wait until after render, in order for the reference
305
+ // element to be in the proper place in the DOM. This is mostly relevant
306
+ // for draggable elements inside portals since they get stamped out in
307
+ // their original DOM position, and then they get transferred to the portal.
308
+ afterNextRender (
309
+ ( ) => {
307
310
this . _updateRootElement ( ) ;
308
311
this . _setupHandlesListener ( ) ;
309
312
310
313
if ( this . freeDragPosition ) {
311
314
this . _dragRef . setFreeDragPosition ( this . freeDragPosition ) ;
312
315
}
313
- } ) ;
314
- } ) ;
316
+ } ,
317
+ { injector : this . _injector } ,
318
+ ) ;
315
319
}
316
320
317
321
ngOnChanges ( changes : SimpleChanges ) {
318
322
const rootSelectorChange = changes [ 'rootElementSelector' ] ;
319
323
const positionChange = changes [ 'freeDragPosition' ] ;
320
324
321
325
// We don't have to react to the first change since it's being
322
- // handled in `ngAfterViewInit` where it needs to be deferred .
326
+ // handled in the `afterNextRender` queued up in the constructor .
323
327
if ( rootSelectorChange && ! rootSelectorChange . firstChange ) {
324
328
this . _updateRootElement ( ) ;
325
329
}
326
330
327
- // Skip the first change since it's being handled in `ngAfterViewInit`.
331
+ // Skip the first change since it's being handled in the `afterNextRender` queued up in the
332
+ // constructor.
328
333
if ( positionChange && ! positionChange . firstChange && this . freeDragPosition ) {
329
334
this . _dragRef . setFreeDragPosition ( this . freeDragPosition ) ;
330
335
}
0 commit comments