Reputation: 2577
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
Upvotes: 0
Views: 447
Reputation: 973
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
Upvotes: 1