'Angular button click with subscriber clean up
If I have a button in the view that calls a function in the controller that gets data via a subscriber from an API service that is an observable, when the observable completes does it clean up the subscriber in the controller or will I have a stack of subscribers for each button click?
View
<button (click)="getData">Get Data</button>
Controller
public getData() {
this.apiService.getTableData()
.subscribe(data => {
this.tableRows = data
})
}
API Service
public getTableData(): Observable<any> {
return new Observable(
observer => {
this.api.get().subscribe(
response => {
observer.next(response);
observer.complete();
},
error => {
observer.error(error);
observer.complete();
}
);
}
);
}
This is stub code, I'm actually passing variables to the API service from the controller and view.
Thanks in advance.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
