Search code examples
angularangular5dragula

DragulaService Action is Repeated


I'm using Dragula in simple task with angular 5, in the first time it works fine but when i'm leaving the page and return again the event logic is repeat twice, when leaving and return again repeat three times and so on... so what is the problem here

ngOnInit() {

 this.dragula.dragend().subscribe(value => {          
              console.log("Service Value "+value);
        });
}

the console message is repeat,I'm trying to destroy it in ngOnDestoy() but still the same problem

this.dragula.dragend().subscribe().unsubscribe();
        if (this.dragula.find('bag-items') !== undefined) {
            this.dragula.destroy('bag-items');
            console.log("Malek destroy")
          }

Solution

  • The solution is to assign the subscription to new variable and destroy it

    this.dragulaEvent=this.dragula.dragend().subscribe(value => {          
                  console.log("Service Value "+value);
            });
    

    in ngOnDestroy()

    this.dragulaEvent.unsubscribe();