We have a simple component with an input property as such:
<app-example [exampleInput$]="exampleInputData$"></app-example>
On the component where we use app-example we have:
get exampleInputData$(): any {
var subject = new Subject<string>();
console.log("Log1");
this.exampleService.getAllData(this.id).subscribe(data => {
console.log("Log2");
subject.next(data);
});
return subject.asObservable();
}
And the exampleService is a simple observable as such:
public getAllData(id): Observable<ExampleObject[]> {
return this.http.get(`${this.baseUrl}data/${id}`).map((response: Response) => {
var data = <ExampleObject[]>response.json();
return data;
}).catch(this.handleError);
}
When we run this it get's into an infinite loop, if we change the changeDetection to OnPush it stops or if we remove the call to getAllData. Does anyone have an idea of why this is causing an infinite loop?
Below is the app-example component and the HTML template:
export class ExampleComponent implements OnDestroy {
private subscription: ISubscription;
private parsedResponse : any;
@Input() exampleInput$;
getData(): void
{
this.subscription = this.exampleInput$.subscribe((data) => {this.parsedResponse = data;},
() => {
// Here we download the data as CSV
});
}
ngOnDestroy() {
if (this.subscription) {
this.subscription.unsubscribe();
}
}
Template:
<button class="btn btn-primary" (click)="getData()">
Download As CSV
</button>
getAllDataandexampleInputData$