I am trying to create a dynamic component that will take api url and intervalPeriod say 'x' minute as input and fetch data every x minute that is passed as input.
Here is my stackblitz that i tried following all the stackoverflow answers..
I need to show data the very first time at 0 seconds and it has to fetch data after that every x second or minute . Does this look right or what am i missing here?
https://stackblitz.com/edit/angular-wzvwmy?file=src%2Fapp%2Fdata-emitter%2Fdata-emitter.component.ts
import { Component, Input, OnInit, Output } from "@angular/core";
import { interval } from "rxjs";
import { flatMap, map } from "rxjs/operators";
import { Http } from "@angular/http";
@Component({
selector: "app-data-emitter",
templateUrl: "./data-emitter.component.html",
styleUrls: ["./data-emitter.component.css"]
})
export class DataEmitterComponent implements OnInit {
@Output() data: any;
@Input() apiUrl: any;
@Input() intervalPeriod: number;
minutes: number;
constructor(private http: Http) {}
ngOnInit() {
this.minutes = this.intervalPeriod * 60 * 1000;
}
subscribes$ = interval(this.minutes)
.pipe(flatMap(() => this.getData()))
//this.getData()
.subscribe(data => {
this.data = data;
console.log(this.data);
});
getData() {
return this.http
.get(this.apiUrl)
.pipe(map((response: any) => response.json()));
}
}