6

I am trying to prevent user to move first element of dragulaService, the code works first time without any errors, but when I leave this page and then open it again, I'm getting error.

The code that is causing the error:

constructor(public service:SmartTablesService, private dragulaService:DragulaService) {
    dragulaService.setOptions('nested-bag', {
      revertOnSpill: true,
      moves: function (el:any, container:any, handle:any):any {
        if (handle.className === 'sorting-table-title') {
          return false;
        } else {
          return true;
        }

      }
    });

The error is:

error_handler.js:48 EXCEPTION: Uncaught (in promise): Error: Error in ./SortTableComponent class SortTableComponent_Host - inline template:0:0 caused by: Bag named: "nested-bag" already exists. Error: Bag named: "nested-bag" already exists. at DragulaService.add (http://platform.local:8080/3.chunk.js:1070:19) at DragulaService.setOptions (http://platform.local:8080/3.chunk.js:1099:24) at new SortTableComponent (http://platform.local:8080/3.chunk.js:1311:24)

Stefan Svrkota
  • 48,787
  • 9
  • 98
  • 87
Vitaly Menchikovsky
  • 7,684
  • 17
  • 57
  • 89

2 Answers2

13

You need to destroy nested-bag in onDestroy lifecycle of your component manually because it's not done automatically:

ngOnDestroy() {
    this.dragulaService.destroy('nested-bag');
}
Stefan Svrkota
  • 48,787
  • 9
  • 98
  • 87
0

It is better to provide unique DragulaService to every Angular component in which drag & drop is used, instead of sharing common service:

@Component({
   ...,
   providers: [DragulaService],
   ...
})
export class MyComponent {
...
}

Source

smartmouse
  • 13,912
  • 34
  • 100
  • 166