title: Angular2 Drag and Drop Target
body: <p>I'm implementing a drag and drop feature in my angular2 application that works like this... A user sees a list of options that they may choose from and a drop zone above the list - when they drag from one of the options into the drop zone then the event is handled.</p>
  
  <p>So far, I have a draggable directive that handles the drag start event and sets some data on the dataTransfer attribute of the event. What I have tried to do is make another directive that is a dropTarget directive that handles simply the drop event and grabs the data.</p>
  
  <p>Is this something that is possible to do? Would I be better just using the same directive for the draggable divs and the drop zone and inserting some logic to only allow the drop event to take place when the div is dropped on the right target.</p>
  
  <p>Thanks </p>
  
  <p>EDIT: Code</p>
  
  <p>Here is the code for my draggable directive:</p>
  
  <pre><code>import {Directive, ElementRef} from '@angular/core';
  
  @Directive({
      selector: '[myDraggable]',
      host: {
          '(dragstart)': 'onDragStart($event)',
          '(dragover)': 'onDragOver($event)',
          '(dragleave)': 'onDragLeave($event)',
          '(dragenter)': 'onDragEnter($event)',
          '(drop)': 'onDrop($event)'
      }
  })
  export class DraggableDirective {
  
      constructor(el: ElementRef) {
          el.nativeElement.setAttribute('draggable', 'true');
      }
  
      onDragStart(ev: DragEvent) {
          console.log("Drag Started");
          ev.dataTransfer.setData('Text', 'Data from drag start');
      }
  
  }
  </code></pre>
  
  <p>and here is the drag target directive code:</p>
  
  <pre><code>import {Directive, ElementRef} from '@angular/core';
  
  @Directive({
      selector: '[dragTarget]',
      host: {
          '(dragover)': 'onDragOver($event)'
      }
  })
  export class DragTargetDirective {
  
      onDragOver(ev: DragEvent) {
          console.log("dragged over target" + ev.dataTransfer.getData('Text'));
      }
  
  }
  </code></pre>
  
  <p>and finally here is the html that contains both directives</p>
  
  <pre><code>&lt;div class="relativeContainer"&gt;
      &lt;div class="absoluteBox" *ngFor="let process of processes" [ngClass]="{'active': process.active}"&gt;
      &lt;div class="process-title"&gt;{{process.stage}} - {{process.name}}&lt;/div&gt;
      &lt;div dragTarget *ngIf="process.options" class="process-selection"&gt;{{process.options[process.selectedOption]}}&lt;/div&gt;
      &lt;ul class="option-list"&gt;
          &lt;li myDraggable *ngFor="let option of process.options" class="option-item"&gt;&lt;p&gt;{{option}}&lt;/p&gt;&lt;/li&gt;
      &lt;/ul&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p></p>
  
  <p>I also have the metadata for the component with the directives imported and declared here</p>
  
  <pre><code>import {DraggableDirective, DragTargetDirective} from '../draggable';
  
  @Component({
      templateUrl: 'app/dashboard/dashboard.component.html',
      styleUrls: [require('./dashboard.component.scss')],
      directives: [DraggableDirective, DragTargetDirective]
  })
  </code></pre>