简体   繁体   English

在动态组件列表上带有ng-template的cdkDropList不起作用

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

I'm trying to use Drag&Drop features released with Angular Material 7. And dynamically creating component using ng-tempalte. 我正在尝试使用Angular Material 7发布的拖放功能,并使用ng-tempalte动态创建组件。

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

and added cdkDrag at components. 并在组件上添加了cdkDrag。

I am using createComponent at parent component to keep on create components. 我在父组件处使用createComponent来继续创建组件。 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. 但是dragdrop功能不起作用,因为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 参考: https : //stackoverflow.com/a/54158293/4481952

Is there a way around to make dropfunctionality work? 有没有办法使dropfunction功能起作用?

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

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和cdkDropList与componenet一起使用,我们必须使用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 请参阅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 用于实施cdkService的Stackblitz代码: https ://stackblitz.com/edit/angular-ngtemplate-reorder-7i6uuk ? file = src%2Fapp%2Fapp.component.ts

hope this helps out someone else. 希望这可以帮助其他人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM