Drag & Drop in Your Angular Apps With ng2-dragula

Dragula is a popular JavaScript library for drag & drop, and ng2-dragula is a wrapper to use Dragula in Angular 2+ apps. Let’s see how it’s used.

Installation

First add ng2-dragula to your project using Yarn or npm:

# Yarn
$ yarn add ng2-dragula

# npm
$ npm install ng2-dragula --save

Now import DragulaModule and add it to your NgModule’s imports:

app.module.ts

// ...

import { DragulaModule } from 'ng2-dragula/ng2-dragula';

@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    // ...
    DragulaModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

And the last setup step is to add the Dragula CSS file to the project. If you’re using the Angular CLI, you can simply add the CSS file path in your .angular-cli.json file:

"styles": [
  "styles.css",
  "../node_modules/dragula/dist/dragula.css"
],

Using ng2-dragula

Now to use it, it’s as simple as using the dragula directive on a container element in your template and providing a group name. Groups in Dragula are called bags:

<ul [dragula]='"bag-items"'>
  <li *ngFor="let item of items"></li>
</ul>

And if you have multiple distinct containers:

<ul [dragula]='"bag-items"'>
  <li *ngFor="let item of items"></li>
</ul>

<ul [dragula]='"bag-people"'>
  <li *ngFor="let person of people"></li>
</ul>

If you want the changes in order to be synced in your items array, simply use the dragulaModel directive with the name of the model. This way, when items are re-ordered via drag & drop, the items array will also have its items re-ordered to reflect the changes:

<ul [dragula]='"bag-items"' [dragulaModel]='items'>
  <li *ngFor="let item of items"></li>
</ul>

Events and Configuration Options

You can use the DragulaService to hook into events or to set configuration options.

Configuration

To use the service, first import it and inject it in the constructor. Then it’s as simple as calling the setOptions method, specifying the container and providing an object literal with your desired options:

import { Component, OnInit } from '@angular/core';

import { DragulaService } from 'ng2-dragula/ng2-dragula';

@Component({ ... })
export class AppComponent implements OnInit {
  constructor(private dragula: DragulaService) {
    this.dragula.setOptions('bag-items', {
      revertOnSpill: true
    });
  }
}

You can set options such as direction, copy, copySortSource, revertOnSpill, removeOnSpill and mirrorContainer. Refer to the project page for configuration options.

Events

You can hook into the drag, drop, over and out events and each of the events return an observable. Here’s a complete example where a message is displayed when dragging or dropping an item:

import { Component, OnInit } from '@angular/core';

import { DragulaService } from 'ng2-dragula/ng2-dragula';

@Component({ ... })
export class AppComponent implements OnInit {
  msg = '';

  items = [
    'Candlestick',
    'Dagger',
    'Revolver',
    'Rope',
    'Pipe',
    'Wrench'
  ];

  constructor(private dragula: DragulaService) { }

  ngOnInit() {
    this.dragula
      .drag
      .subscribe(value => {
        this.msg = `Dragging the ${ value[1].innerText }!`;
      });

    this.dragula
      .drop
      .subscribe(value => {
        this.msg = `Dropped the ${ value[1].innerText }!`;

        setTimeout(() => {
          this.msg = '';
        }, 1000);
      });
  }
}

The value returned by both the drag and the drop event observables is an array containing the bag name (e.g.: bag-items), the dragged element and the container element (the <ul> element).

Our App at Work! 🎥

Example of using ng2-dragula

✖ Clear

🕵 Search Results

🔎 Searching...