【发布时间】:2019-09-30 04:11:01
【问题描述】:
这是我在 SO 上提出的上一个问题的延续: Add directives to component selector when it is declared - Angular 7
我在单击按钮时动态创建组件。组件以类似列表的方式在另一个下方显示。我想引入拖放行为,以便用户可以在创建组件后重新排列组件。
在上一个问题中,我尝试使用 Angular-Material,但意识到可能无法将它用于组件,因为在组件的选择器标签中添加“cdkDrag”指令的问题,以及 cdkDropList和 cdkDrag 可能需要在同一个模板中。
我在模板中有一个这样的 div:
<div cdkDropList style="margin: 20px" (cdkDropListDropped)="drop($event)">
<div #container></div>
</div>
而且,我正在按如下方式创建自定义组件:
@ViewChild('container', {read: ViewContainerRef})
container: ViewContainerRef;
const childComponent = this.componentFactoryResolver.resolveComponentFactory(CustomComponent);
const component = this.container.createComponent(childComponent);
这很好用。是否有可能创建可拖动的动态创建的组件?
谢谢。
【问题讨论】:
标签: javascript html angular drag-and-drop angular-components