Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Template parse errors: There is no directive with "exportAs" set to "cdkDropList"

I am trying to use Angular Material for drag and drop as shown in Transferring items between lists.

I am trying the exact same code shown in the example but getting this error in console:

Uncaught Error: Template parse errors: There is no directive with "exportAs" set to "cdkDropList"

Below is my code, its the same as shown in link, I am sharing anyways:

TS:

import {Component} from '@angular/core';
import {CdkDragDrop, moveItemInArray, transferArrayItem} from '@angular/cdk/drag-drop';

/**
 * @title Drag&Drop connected sorting
 */
@Component({
  selector: 'cdk-drag-drop-connected-sorting-example',
  templateUrl: 'cdk-drag-drop-connected-sorting-example.html',
  styleUrls: ['cdk-drag-drop-connected-sorting-example.css'],
})
export class CdkDragDropConnectedSortingExample {
  todo = [
    'Get to work',
    'Pick up groceries',
    'Go home',
    'Fall asleep'
  ];

  done = [
    'Get up',
    'Brush teeth',
    'Take a shower',
    'Check e-mail',
    'Walk dog'
  ];

  drop(event: CdkDragDrop<string[]>) {
    if (event.previousContainer === event.container) {
      moveItemInArray(event.container.data, event.previousIndex, event.currentIndex);
    } else {
      transferArrayItem(event.previousContainer.data,
                        event.container.data,
                        event.previousIndex,
                        event.currentIndex);
    }
  }
}

HTML:

<div class="example-container">
  <h2>To do</h2>

  <div
    cdkDropList
    #todoList="cdkDropList"
    [cdkDropListData]="todo"
    [cdkDropListConnectedTo]="[doneList]"
    class="example-list"
    (cdkDropListDropped)="drop($event)">
    <div class="example-box" *ngFor="let item of todo" cdkDrag>{{item}}</div>
  </div>
</div>

<div class="example-container">
  <h2>Done</h2>

  <div
    cdkDropList
    #doneList="cdkDropList"
    [cdkDropListData]="done"
    [cdkDropListConnectedTo]="[todoList]"
    class="example-list"
    (cdkDropListDropped)="drop($event)">
    <div class="example-box" *ngFor="let item of done" cdkDrag>{{item}}</div>
  </div>
</div>
like image 404
Thanthu Avatar asked Mar 30 '19 15:03

Thanthu


2 Answers

As stated in the API you need to import DragDropModule in your module

import {DragDropModule} from '@angular/cdk/drag-drop';

@NgModule({
  ...
  imports: [
    ...
    DragDropModule
    ...
like image 196
Flyii Avatar answered Nov 13 '22 23:11

Flyii


an extra answer for those new to angular world. in angular Use the hash symbol (#) to declare a reference variable. The following reference variable, #phone, declares a phone variable on an element.

<input #phone placeholder="phone number" />

A template reference variable is often a reference to a DOM element within a template. It can also refer to a directive (which contains a component), an element, TemplateRef, or a web component. In most cases, Angular sets the reference variable's value to the element on which it is declared. but if you want to use a directive. it should be declared in reference variable value like below example

<form #itemForm="ngForm" (ngSubmit)="onSubmit(itemForm)">
  <label for="name"
    >Name <input class="form-control" name="name" ngModel required />
  </label>
  <button type="submit">Submit</button>
</form>

ngForm is a directive . and it should be known to angular compiler. and in the question

<div
    cdkDropList
    #todoList="cdkDropList"
    [cdkDropListData]="todo"
    [cdkDropListConnectedTo]="[doneList]"
    class="example-list"
    (cdkDropListDropped)="drop($event)">
    <div class="example-box" *ngFor="let item of todo" cdkDrag>{{item}}</div>
  </div>
</div>

cdkDropList is a directive too. therefore it should be known to compiler and therefore its module should be imported to the module it is used

like image 1
Stef Avatar answered Nov 13 '22 23:11

Stef