0

I have a URL with data in json format and I get it with the HttpClient and it works good.

I binded the function

showConfig()

to get the data to a button and when pressed I get the newest data.

BUT: I have to click again to get the newest data.

Can I make it refresh automatically, like with Firebase?

url = myURL;
constructor(private http: HttpClient) { }

getConfig() {
   return this.http.get(this.url);
}

showConfig(){
   this.getConfig().subscribe(data => console.log(data))
}

an

OcK
  • 93
  • 13
  • If your data is bound to the view, an update on that data should automatically trigger angular's change detection and therefore result in a rerender, unless you are having some special cases where angular doesn't understand that something has changed. I think I had some trouble with updating arrays for example, although I'm not sure this is the case. In your example the data is only logged to the console, is it set to a component variable in your real code? – Benedikt Schmidt May 10 '18 at 01:43
  • You can use `IntervalObservable`, `TimerObservable` of rxjs. For a complete answer refer to [this post](https://stackoverflow.com/questions/35316583/angular2-http-at-an-interval) – Senal May 10 '18 at 02:03
  • @BenediktSchmidt it is also bound to the view via a
  • – OcK May 10 '18 at 10:15
  • could you show us some of your view code so we can understand what type of data we are talking about here? – Benedikt Schmidt May 10 '18 at 10:17
  • @BenediktSchmidt Imagine it's an object with an array of strings in it. – OcK May 10 '18 at 13:02
  • Welp, that's probably not enough information to actually find out what the problem is. As a little trick you could try to inject `ChangeDetectorRef` and call `ChangeDetectorRef.detectChanges()` inside your `subscribe`function right after setting the data to your component varable. – Benedikt Schmidt May 10 '18 at 13:12