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

时间:2017-01-12 19:53:26

标签: angular typescript ionic2

我的目标是,每次加载标签视图时,我的代码都会获得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);
            })
          }
        }

  }

0 个答案:

没有答案