Search code examples
angulardrag-and-dropng2-dragula

Getting error second time getting to page with ng2-dragula


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)


Solution

  • 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');
    }