Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bug(Drag and Drop): Drag/drop between connected DropLists in different *ngFor blocks throws error #22813

Closed
AustinHaigh-Hach opened this issue May 26, 2021 · 3 comments · Fixed by #22904
Assignees
Labels
area: cdk/drag-drop P3 An issue that is relevant to core functions, but does not impede progress. Important, but not urgent

Comments

@AustinHaigh-Hach
Copy link

Reproduction

Example (see example-component): https://stackblitz.com/edit/components-issue-wv8n6t

Steps to reproduce:

  1. Create 2 *ngFor blocks, each with (at least 1) cdkDropList. Each cdkDropList should contain a single cdkDrag.
  2. All DropLists should be connected to eachother (cdkDropListConnectedTo).
  3. Drag/drop a cdkDrag element from a DropList in the 1st *ngFor block to a DropList in the 2nd *ngFor block (or vice versa).

Expected Behavior

The cdkDrag element should be moved into the DropList, and no exceptions should be thrown.

Actual Behavior

An exception is thrown (below) and the drag/drop operation is terminated (the cdkDrag element is returned to the last DropList it was in). Note: a slightly different exception is thrown in the stackblitz example above. This exception is thrown when I run that code locally.

zone.js:182 Uncaught TypeError: Cannot read property '_startScrollingIfNecessary' of undefined
at DragRef._updateActiveDropContainer (drag-drop.js:945)
at SafeSubscriber.DragRef._pointerMove [as _next] (drag-drop.js:451)
at SafeSubscriber.__tryOrUnsub (Subscriber.js:183)
at SafeSubscriber.next (Subscriber.js:122)
at Subscriber._next (Subscriber.js:72)
at Subscriber.next (Subscriber.js:49)
at Subject.next (Subject.js:39)
at HTMLDocument.handler (drag-drop.js:2464)
at ZoneDelegate.invokeTask (zone.js:406)
at Zone.runTask (zone.js:178)

Environment

  • Angular: 12.0.1
  • CDK/Material: 12.0.1
  • Browser(s): Chromium Version 90.0.4430.212 (Official Build) snap (64-bit)
  • Operating System (e.g. Windows, macOS, Ubuntu): Ubuntu 20.04 LTS
@AustinHaigh-Hach AustinHaigh-Hach added the needs triage This issue needs to be triaged by the team label May 26, 2021
@AustinHaigh-Hach
Copy link
Author

AustinHaigh-Hach commented May 26, 2021

This appears to be similar to issue #19241, but PR #19243 doesn't fix this bug.

@AustinHaigh-Hach
Copy link
Author

AustinHaigh-Hach commented May 27, 2021

After further investigation, it appears this bug has something to do with the ngFor directive, specifically the ngDoCheck hook (this calls CdkDrag.onDestroy and eventually undefines DragRef._preview - the exception is thrown when DragRef._preview.style is accessed later).

I wrote a very simple ngFor "clone" (below) and an exception is no longer thrown when I use it instead of *ngFor in the example above, but I'm seeing a different issue now. With my custom *appFor directive, sorting doesn't work between DropLists, even if they are all created by the same *appFor loop (ngFor works as expected when all DropLists are in the same group).

@Directive({
  selector: '[appFor]'
})
export class ForDirective {
  @Input() appForOf : any;

  constructor(private templateRef:TemplateRef<any>,
              private viewContainerRef:ViewContainerRef) {
  }

  ngOnInit(){
    this.appForOf.forEach((key, i)=> this.viewContainerRef.createEmbeddedView(this.templateRef, {index:i}));
  }
}

@crisbeto crisbeto added area: cdk/drag-drop needs investigation A member of the team needs to do further investigation to determine the root cause P3 An issue that is relevant to core functions, but does not impede progress. Important, but not urgent has pr and removed needs triage This issue needs to be triaged by the team needs investigation A member of the team needs to do further investigation to determine the root cause labels Jun 2, 2021
crisbeto added a commit to crisbeto/material2 that referenced this issue Jun 6, 2021
… the `entered` event

When an item enters a new drop container, we dispatch an event and run some logic afterwards. The problem is that if the item is destroyed as a result of the event, the logic after the event will throw because dragging was interrupted.

Fixes angular#22813.
wagnermaciel pushed a commit that referenced this issue Jun 10, 2021
… the `entered` event (#22904)

When an item enters a new drop container, we dispatch an event and run some logic afterwards. The problem is that if the item is destroyed as a result of the event, the logic after the event will throw because dragging was interrupted.

Fixes #22813.
wagnermaciel pushed a commit that referenced this issue Jun 10, 2021
… the `entered` event (#22904)

When an item enters a new drop container, we dispatch an event and run some logic afterwards. The problem is that if the item is destroyed as a result of the event, the logic after the event will throw because dragging was interrupted.

Fixes #22813.

(cherry picked from commit 0431d81)
@angular-automatic-lock-bot
Copy link

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Jul 11, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area: cdk/drag-drop P3 An issue that is relevant to core functions, but does not impede progress. Important, but not urgent
Projects
None yet
2 participants