如何在api值更改时更新http get

时间:2017-12-15 21:17:01

标签: angular api typescript ionic-framework

我正在调用一个不时更改值的API,我想要反映值更改,而不会在我的应用中点击刷新。我是打字稿和角度的新手,但我很快就赶上了。这就是我所拥有的。

服务提供商

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/');
  }

}

TypeScript文件

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();
  }
}

我需要一种刷新/更新api调用值的方法。有什么建议?

0 个答案:

没有答案