2

I'm calling an API that changes values every now and then and i want the value changes to be reflected without hitting refresh in my app. Im new to typescript and angular but i catch on quick. Here's what i have.

Service Provider

import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';

@Injectable()
export class CryptoServiceProvider {

  constructor(public http: HttpClient) {        
  }

  getCrypto() {
    return this.http.get('https://api.coinmarketcap.com/v1/ticker/');
  }

}

The TypeScript File

import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams, PopoverController } from 'ionic-angular';
import {CurrpopPage} from "../currpop/currpop";
import { Storage } from '@ionic/storage';
import { ViewChild } from '@angular/core';
import { Slides } from 'ionic-angular';
import {HttpClient} from "@angular/common/http";
import { Observable } from 'rxjs/Observable';
import { CryptoServiceProvider } from '../../providers/crypto-service/crypto-service'

@IonicPage()
@Component({
  selector: 'page-favorites',
  templateUrl: 'favorites.html',
})
export class FavoritesPage {
  @ViewChild(Slides) slides: Slides;
  currSymbol:string;
  data:any;
  myfavcurrency: Array<any> = [];
  posts: Observable<any>;

  constructor(public navCtrl: NavController,
          public navParams: NavParams,
          public popoverCtrl: PopoverController,
          public http: HttpClient,
          private storage: Storage,
          public apiProvider: CryptoServiceProvider) {

      this.currSymbol = '$';

      this.storage.get('fav').then((val) => {
          this.myfavcurrency = val;          
      });

      //I need update posts as api value changes
      this.posts = this.apiProvider.getCrypto();
  }

  ionViewDidLoad() {    
    this.currSymbol = '$';
  }

  task(){
    this.navCtrl.push('TaskPage');
  }

  slideToPrev(){
    this.slides.slidePrev();
  }
  slideToNext(){
    this.slides.slideNext();
  }
}

I need a means of refreshing/updating the values from the api call. Any suggestions?

Elvin Opara
  • 195
  • 1
  • 1
  • 9
  • It sort of depends ... you could just do it on an interval with `setTimeout` or something for example, or even `Observable.interval` but you may also want to use web sockets if that's an option. – Explosion Pills Dec 15 '17 at 21:36
  • Was about to say Observable.interval would be your best bet here in terms of what you're trying to achieve. Another option would be Redux. – Chau Tran Dec 15 '17 at 21:37
  • check your solution here [Angular2/4: Refresh Data Realtime](https://stackoverflow.com/a/44947870) – Nick B Dec 15 '17 at 21:40
  • i've been hitting my head on the wall about this Observable.interval. Are there any resources you can point me to? or a simple example will suffice – Elvin Opara Dec 15 '17 at 21:48
  • @ElvinOpara Here is a basic [example](https://stackblitz.com/edit/angular-mwffzo?file=app%2Fapp.component.ts) using `timer` (similar to interval but with an initial call) – LLai Dec 15 '17 at 22:58

0 Answers0