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

cdkDrag does nothing when inside an *ngFor looping through a returned 2 dimensional array #15807

Closed
wshaver opened this Issue Apr 12, 2019 · 3 comments

Comments

Projects
None yet
3 participants
@wshaver
Copy link

wshaver commented Apr 12, 2019

What is the expected behavior?

cdkDrag works from inside an ngFor looping through an array with a nested array.

What is the current behavior?

Nothing happens. cdkDrag does not move or drag.

What are the steps to reproduce?

https://stackblitz.com/edit/angular-a4ftm7-oectv4?file=app%2Fcdk-drag-drop-connected-sorting-example.html

<div *ngFor="let i of getNestedArray()">
  complete failure :(
  <div cdkDrag>1</div>
  <div cdkDrag>2</div>
  <div cdkDrag>3</div>
</div>
 getNestedArray(){
   return [[]];
 }

Which versions of Angular, Material, OS, TypeScript, browsers are affected?

chrome browser, angular 7.1.0 and 7.2.0, material 7.1.0 and 7.3.7

Is there anything else we should know?

Happy to explain more if this isn't clear. Had me stumped for hours. :(

@Airblader

This comment has been minimized.

Copy link
Contributor

Airblader commented Apr 13, 2019

This is because calling getNestedArray, a function, creates the nested array from scratch on every change detection cycle. While this is also true for getSimpleArray, the difference is that the inner array is also recreated everytime, meaning it's a new reference.

Because of that, the draggable item is recreated on every CD cycle, which resets its position. This can be proven easily by making the component OnPush making the items draggable.

So this is actually not a bug, but just incorrect usage. Generally you just shouldn't call functions from templates, but if you must, specifying a trackBy function will help you here.

@crisbeto

This comment has been minimized.

Copy link
Member

crisbeto commented Apr 13, 2019

@Airblader's explanation is correct. Closing since this works as expected.

@crisbeto crisbeto closed this Apr 13, 2019

@wshaver

This comment has been minimized.

Copy link
Author

wshaver commented Apr 13, 2019

Thanks for the detailed explanation, makes sense.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.