Angular 如何使用ionic 2正确执行http请求?

Angular 如何使用ionic 2正确执行http请求?,angular,typescript,ionic2,Angular,Typescript,Ionic2,我的目标是,每次加载选项卡视图时,我的代码都会获取json数据。因此,当我编辑数据时,用户将从服务器获取新数据。目前,我必须再次构建应用程序,然后在设备上再次运行它。如果我不这样做,数据就不会改变 它在浏览器上运行良好 这是我目前的代码。我应该如何改进这一点 import { Component } from '@angular/core'; import { NavController, NavParams, LoadingController } from 'ionic-angular';

我的目标是,每次加载选项卡视图时,我的代码都会获取json数据。因此,当我编辑数据时,用户将从服务器获取新数据。目前,我必须再次构建应用程序,然后在设备上再次运行它。如果我不这样做,数据就不会改变

它在浏览器上运行良好

这是我目前的代码。我应该如何改进这一点

import { Component } from '@angular/core';
import { NavController, NavParams, LoadingController } from 'ionic-angular';
import {Http} from '@angular/http';
import 'rxjs/add/operator/map';

@Component({
  selector: 'page-book',
  templateUrl: 'book.html'
})
export class BookPage {

  ionViewWillEnter() {
    this.makeGetRequest();
  }


    private items: any;
    private verbs: any;

    private searchQuery: string = '';
    private myInput: any;

    constructor(public navCtrl: NavController, private http: Http, private loadingCtrl: LoadingController) {
      this.myInput = '';
      this.http = http;
    }

        initializeItems() {
          this.items = this.verbs;
      alert("init items");// runs
        }

        makeGetRequest() {
          alert("maker"); // runs
          this.http.get('https://domain.ch/b2/words.json').map(res => res.json()).subscribe(data => {
              this.verbs = data;
              this.initializeItems();
          });
        }
        getItems(ev: any) {
          // Reset items back to all of the items
          this.initializeItems();

          // set val to the value of the searchbar
          let val = ev.target.value;

          // if the value is an empty string don't filter the items
          if (val && val.trim() != '') {
            this.items = this.items.filter((item) => {
              return (item.de.toLowerCase().indexOf(val.toLowerCase()) > -1);
            })
          }
        }

  }

客户端是否正在更改数据?如果是这样的话,您只需要在发生这种情况时再次调用getItems。否则,您可以设置中继器以保持轮询或使用类似socket.io的内容。您可能需要查看ngZone