This might be a basic question, but didn't found any answer which is sufficient. So that's why I thought creating a SO.
I have a form which has a submit handler.
<form class="form login" [formGroup]="loginForm" (ngSubmit)="onSubmit()" novalidate>
On submitting I call a function which subscribes to an observable, that executes a HTTP call to firebase (using the AngularFire2 library).
public onSubmit() : void {
const values = this.loginForm.value;
let subscription = this.loginUsernamePassword(values.email, values.password).subscribe((data) => {
this.processLogin(data);
}, (error) => {
console.log("erreur" + error);
}, () => {
console.log("one");
});
}
Now suppose that I submit the form multiple times, then multiple subscriptions are created. What I'm concerned about is data cleanup (unsubscribe), but don't know what is the best way to handle this, especially because this a one-shot function. I see two solutions.
1) Store the subscription in a collection of subscriptions, which gets destroyed 'ngOnDestroy' when the component is removed. But I think for this situation: a single HTTP call, it's a bit weird.
2) Unsubscribe immediately after the oberservable completed.
3) .. any other?
Thanks for you help and tips.