重新实例化数据时,Angular CDK 拖放列表会中断

Posted

技术标签:

【中文标题】重新实例化数据时,Angular CDK 拖放列表会中断【英文标题】:Angular CDK drag and drop lists break when their data is re-instantiated 【发布时间】:2019-07-26 01:33:55 【问题描述】:

我有一个包含多个表(使用 mat-table 指令)的 cdkDropListGroup,我正在尝试设置拖放,以便可以重新排列表中的元素或将其拖到另一个表中。但是,当重新实例化拖放列表的数据时,项目不会拖动,并且会在控制台中记录错误。 Here 是一个重新创建问题的堆栈闪电战。尝试在表之间或表内拖动行几次,应该会出现错误,并且每隔几次尝试拖放就会卡住。如何解决这个问题,因为在我的实际代码中,这些数据来自某个 http 服务并且必须重新实例化,并且每次将项目拖放到新位置时都必须重新填充表?

https://stackblitz.com/edit/angular-e3ajdt

这是我看到的错误。

    zone.js:192 Uncaught TypeError: Cannot read property '_getSiblingContainerFromPosition' of undefined
    at DragRef._updateActiveDropContainer (drag-ref.ts:727)
    at SafeSubscriber.DragRef._pointerMove [as _next] (drag-ref.ts:505)
    at SafeSubscriber.__tryOrUnsub (Subscriber.ts:270)
    at SafeSubscriber.next (Subscriber.ts:212)
    at Subscriber._next (Subscriber.ts:141)
    at Subscriber.next (Subscriber.ts:101)
    at Subject.next (Subject.ts:68)
    at htmlDocument.handler (drag-drop-registry.ts:127)
    at ZoneDelegate.invokeTask (zone.js:421)
    at Zone.runTask (zone.js:188)
        DragRef._updateActiveDropContainer @ drag-ref.ts:727
        DragRef._pointerMove @ drag-ref.ts:505
        SafeSubscriber.__tryOrUnsub @ Subscriber.ts:270
        SafeSubscriber.next @ Subscriber.ts:212
        Subscriber._next @ Subscriber.ts:141
        Subscriber.next @ Subscriber.ts:101
        Subject.next @ Subject.ts:68
handler @ drag-drop-registry.ts:127
        ZoneDelegate.invokeTask @ zone.js:421
        Zone.runTask @ zone.js:188
        ZoneTask.invokeTask @ zone.js:496
        invokeTask @ zone.js:1540
        globalZoneAwareCaptureCallback @ zone.js:1598
        setTimeout (async)
        scheduleTask @ zone.js:2075
        ZoneDelegate.scheduleTask @ zone.js:407
        Zone.scheduleTask @ zone.js:232
        Zone.scheduleMacroTask @ zone.js:255
        scheduleMacroTaskWithCurrentZone @ zone.js:1114
        (anonymous) @ zone.js:2090
        proto.(anonymous function) @ zone.js:1394
        hostReportError @ hostReportError.ts:7
        SafeSubscriber.__tryOrUnsub @ Subscriber.ts:276
SafeSubscriber.next @ Subscriber.ts:212
Subscriber._next @ Subscriber.ts:141
Subscriber.next @ Subscriber.ts:101
Subject.next @ Subject.ts:68
handler @ drag-drop-registry.ts:127
ZoneDelegate.invokeTask @ zone.js:421
Zone.runTask @ zone.js:188
ZoneTask.invokeTask @ zone.js:496
invokeTask @ zone.js:1540
globalZoneAwareCaptureCallback @ zone.js:1598
preview-ee4d58d6ef0576b4e139b.js:1 Unhandled Promise rejection: Cannot read property 'getItemIndex' of undefined ; Zone: <root> ; Task: Promise.then ; Value: TypeError: Cannot read property 'getItemIndex' of undefined
    at eval (drag-ref.ts:710)
    at ZoneDelegate.invoke (zone.js:388)
    at Object.onInvoke (ng_zone.ts:273)
    at ZoneDelegate.invoke (zone.js:387)
    at Zone.run (zone.js:138)
    at NgZone.run (ng_zone.ts:171)
    at DragRef._cleanupDragArtifacts (drag-ref.ts:700)
    at eval (drag-ref.ts:577)
    at ZoneDelegate.invoke (zone.js:388)
    at Zone.run (zone.js:138) TypeError: Cannot read property 'getItemIndex' of undefined
    at eval (https://angular-e3ajdt.stackblitz.io/turbo_modules/@angular/cdk@7.3.0/bundles/cdk-drag-drop.umd.js:895:56)
    at ZoneDelegate.invoke (https://angular-e3ajdt.stackblitz.io/turbo_modules/zone.js@0.8.26/dist/zone.js:388:26)
    at Object.onInvoke (https://angular-e3ajdt.stackblitz.io/turbo_modules/@angular/core@7.1.0/bundles/core.umd.js:16186:37)
    at ZoneDelegate.invoke (https://angular-e3ajdt.stackblitz.io/turbo_modules/zone.js@0.8.26/dist/zone.js:387:32)
    at Zone.run (https://angular-e3ajdt.stackblitz.io/turbo_modules/zone.js@0.8.26/dist/zone.js:138:43)
    at NgZone.run (https://angular-e3ajdt.stackblitz.io/turbo_modules/@angular/core@7.1.0/bundles/core.umd.js:16100:32)
    at DragRef._cleanupDragArtifacts (https://angular-e3ajdt.stackblitz.io/turbo_modules/@angular/cdk@7.3.0/bundles/cdk-drag-drop.umd.js:883:22)
    at eval (https://angular-e3ajdt.stackblitz.io/turbo_modules/@angular/cdk@7.3.0/bundles/cdk-drag-drop.umd.js:363:23)
    at ZoneDelegate.invoke (https://angular-e3ajdt.stackblitz.io/turbo_modules/zone.js@0.8.26/dist/zone.js:388:26)
    at Zone.run (https://angular-e3ajdt.stackblitz.io/turbo_modules/zone.js@0.8.26/dist/zone.js:138:43)

【问题讨论】:

【参考方案1】:

通过每次在 populateDataSource 方法中重新实例化数据时注入 angular 的 ChangeDetectorRef 并调用 detectChanges() 来解决此问题。

【讨论】:

如果您查看 stackblitz table-expended-rows-example.ts 第 73 行。每次填充数据时都会调用 changeDetectorRef.detectChanges()。当数据被初始化(例如,如果你从后端或其他地方获取它)以及每次删除一个项目时都会发生这种情况stackblitz.com/edit/angular-e3ajdt

以上是关于重新实例化数据时,Angular CDK 拖放列表会中断的主要内容,如果未能解决你的问题,请参考以下文章

在 Angular Material CDK 拖放中,如何防止项目随着元素移动而自动重新排列?

Angular CDK拖放,保持顺序直到下降

使用 cdkDropList 时元素样式不适用(角度 cdk 拖放)

使用角材料的拖放重新排列垫表行

角度材质嵌套拖放

Angular CDK 拖放:不要移动源项目