angualr2以上版本
    我使用的是angualr6.x最新版
    https://github.com/valor-software/ng2-dragula

    1. 1.安装依赖
    2. npm install ng2-dragula
    3. # or
    4. yarn add ng2-dragula
    5. 2.添加这一行到你的 polyfills.ts:
    6. (window as any).global = window;
    7. 3.引入模块 DragulaModule.forRoot() 如下:
    8. import { DragulaModule } from 'ng2-dragula';
    9. @NgModule({
    10. imports: [
    11. ...,
    12. DragulaModule.forRoot()
    13. ],
    14. })
    15. export class AppModule { }
    16. 4.引入公用css node_modules/dragula/dist/dragula.css
    17. 或者直接复制所有cssstyles.scss文件
    18. 5.使用指令,给一个自定义名称,随意的字符串就行,标识一个div的内容可以拖拽,dragula="div1" [dragula]="Vampires"意义等同 代码如下
    19. <ul dragula="div1">
    20. <li>Dracula</li>
    21. <li>Kurz</li>
    22. <li>Vladislav</li>
    23. <li>Deacon</li>
    24. </ul>
    25. 6.多个div需要拖拽如下
    26. <div dragula="div1">
    27. </div>
    28. <div dragula="div2">
    29. </div>
    30. <div dragula="div3">
    31. </div>
    32. 7. 如果需要双向绑定拖拽的数据 [(dragulaModel)]
    33. [(dragulaModel)]等同于 [dragulaModel]="vampires" (dragulaModelChange)="vampires = $event" 类似ng自带的[(ngModel)]
    34. <ul dragula="VAMPIRES" [(dragulaModel)]="vampires">
    35. <li *ngFor="let vamp of vampires">
    36. {{ vamp.name }} likes {{ vamp.favouriteColor }}
    37. </li>
    38. </ul>
    39. 等同于
    40. <ul dragula="VAMPIRES" [dragulaModel]="vampires" (dragulaModelChange)="vampires = $event">
    41. ...
    42. </ul>
    43. 8.拖拽过程中的事件订阅
    44. import { Subscription } from 'rxjs';
    45. import { DragulaService } from 'ng2-dragula';
    46. export class MyComponent {
    47. // RxJS Subscription is an excellent API for managing many unsubscribe calls.
    48. // See note below about unsubscribing.
    49. subs = new Subscription();
    50. constructor(private dragulaService: DragulaService) {
    51. // These will get events limited to the VAMPIRES group.
    52. this.subs.add(this.dragulaService.drag("VAMPIRES")
    53. .subscribe(({ name, el, source }) => {
    54. // ...
    55. })
    56. );
    57. this.subs.add(this.dragulaService.drop("VAMPIRES")
    58. .subscribe(({ name, el, target, source, sibling }) => {
    59. // ...
    60. })
    61. );
    62. // some events have lots of properties, just pick the ones you need
    63. this.subs.add(this.dragulaService.dropModel("VAMPIRES")
    64. // WHOA
    65. // .subscribe(({ name, el, target, source, sibling, sourceModel, targetModel, item }) => {
    66. .subscribe(({ sourceModel, targetModel, item }) => {
    67. // ...
    68. })
    69. );
    70. // You can also get all events, not limited to a particular group
    71. this.subs.add(this.dragulaService.drop()
    72. .subscribe(({ name, el, target, source, sibling }) => {
    73. // ...
    74. })
    75. );
    76. }
    77. ngOnDestroy() {
    78. // destroy all the subscriptions at once
    79. this.subs.unsubscribe();
    80. }
    81. }

    还有一个非常棒的插件可以看看
    https://github.com/xieziyu/angular2-draggable