cdkDropList with ng-template on dynamic component list do'snt work

帅比萌擦擦* 提交于 2020-04-30 16:04:00

问题


I'm trying to use Drag&Drop features released with Angular Material 7. And dynamically creating component using ng-tempalte.

<div cdkDropList (cdkDropListDropped)="dropLocal($event)"> <ng-template #components></ng-template> </div>

and added cdkDrag at components.

I am using createComponent at parent component to keep on create components. viewContainerRef.createComponent

but dragdrop functionality is not functional since cdkDrag must be in a tag nested inside the one with cdkDropList, otherwise the dragged element won't detect the drop zone.

reference :https://stackoverflow.com/a/54158293/4481952

Is there a way around to make dropfunctionality work?

demo code https://stackblitz.com/edit/angular-ngtemplate-reorder?file=src%2Fapp%2Fapp.component.html


回答1:


The problem is you can't use cdkDrag and cdkDropList with componenet , we have to use service for that which is recently released with AngularMaterial 3.5.+

CdkDrag uses dependency injection to find out whether it's inside a CdkDropList which doesn't work when the template is inside a different component. For more advanced cases like this one you can use the DragDrop service to attach the drag&drop functionality yourself.

Refer https://github.com/angular/material2/issues/15553

Stackblitz code for implementation of cdkService: https://stackblitz.com/edit/angular-ngtemplate-reorder-7i6uuk?file=src%2Fapp%2Fapp.component.ts

hope this helps out someone else.



来源:https://stackoverflow.com/questions/55225625/cdkdroplist-with-ng-template-on-dynamic-component-list-dosnt-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!