0

from a controller I input a parameter to a subject

  selectImport(event){
      this.importacionService.onChildSelectImportChanged.next(event);
  }

and from my component importacion.component.ts I subscribe to this subject

this.importacionService.onSelectImportChanged.subscribe( addImport => {

    this.importacionService.addImport(this.month, addImport);
   });


  }

Now everything is fine but when I close the dialog of importacion.component.ts and I'll do it again, the subject creates a second suscbripcion and therefore 2 observers, then when I enter a new data with .next() the subscriber runs twice .

how can I control this behavior, my expected behavior is that only exist and parameter in my subject and only run when its value changes

Luis Ruiz Figueroa
  • 2,537
  • 3
  • 18
  • 23

1 Answers1

1

Put this in your component

private subscription: Subscription;

ngOnInit() {
    this.subscription = this.importacionService.onSelectImportChanged.subscribe(...);
}

ngOnDestroy() {
    if (this.subscription){
        this.subscription.unsubscribe();
    }
}

You need to unsubscribe from observables you create or manage yourself

bygrace
  • 5,868
  • 1
  • 31
  • 58
YourGoodFriend
  • 943
  • 15
  • 22