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 · 4 comments

Comments

@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
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
Copy link
Member

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

@wshaver
Copy link
Author

wshaver commented Apr 13, 2019

Thanks for the detailed explanation, makes sense.

@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 Sep 10, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants