diff --git a/src/cdk/drag-drop/directives/drag-placeholder.ts b/src/cdk/drag-drop/directives/drag-placeholder.ts index e6df55b485e8..5afbfe04bf8c 100644 --- a/src/cdk/drag-drop/directives/drag-placeholder.ts +++ b/src/cdk/drag-drop/directives/drag-placeholder.ts @@ -26,16 +26,16 @@ export const CDK_DRAG_PLACEHOLDER = new InjectionToken('CdkD providers: [{provide: CDK_DRAG_PLACEHOLDER, useExisting: CdkDragPlaceholder}], }) export class CdkDragPlaceholder implements OnDestroy { - private _drag = inject(CDK_DRAG_PARENT); + private _drag = inject(CDK_DRAG_PARENT, {optional: true}); /** Context data to be added to the placeholder template instance. */ @Input() data: T; constructor(public templateRef: TemplateRef) { - this._drag._setPlaceholderTemplate(this); + this._drag?._setPlaceholderTemplate(this); } ngOnDestroy(): void { - this._drag._resetPlaceholderTemplate(this); + this._drag?._resetPlaceholderTemplate(this); } } diff --git a/src/cdk/drag-drop/directives/drag-preview.ts b/src/cdk/drag-drop/directives/drag-preview.ts index 9c23d8a03bf2..73a8c17219f5 100644 --- a/src/cdk/drag-drop/directives/drag-preview.ts +++ b/src/cdk/drag-drop/directives/drag-preview.ts @@ -34,7 +34,7 @@ export const CDK_DRAG_PREVIEW = new InjectionToken('CdkDragPrevi providers: [{provide: CDK_DRAG_PREVIEW, useExisting: CdkDragPreview}], }) export class CdkDragPreview implements OnDestroy { - private _drag = inject(CDK_DRAG_PARENT); + private _drag = inject(CDK_DRAG_PARENT, {optional: true}); /** Context data to be added to the preview template instance. */ @Input() data: T; @@ -43,10 +43,10 @@ export class CdkDragPreview implements OnDestroy { @Input({transform: booleanAttribute}) matchSize: boolean = false; constructor(public templateRef: TemplateRef) { - this._drag._setPreviewTemplate(this); + this._drag?._setPreviewTemplate(this); } ngOnDestroy(): void { - this._drag._resetPreviewTemplate(this); + this._drag?._resetPreviewTemplate(this); } }