【问题标题】:How to get Data in List in Ionic hybrid application如何在 Ionic 混合应用程序中获取列表中的数据
【发布时间】:2017-12-12 11:56:34
【问题描述】:

我是 Ionic 混合开发的初学者。我想在我的项目中解析 List 中的数据。 我正在使用这个网络服务:http://api.geonames.org/earthquakesJSON?north=44.1&south=-9.9&east=-22.4&west=55.2&username=bertt。 我想在列表中显示所有日期。这是 json 响应

{ "earthquakes": [ { "datetime": "2011-03-11 04:46:23", "depth": 24.4, "lng": 142.369, "src": "us", "eqid": "c0001xgp", "magnitude": 8.8, "lat": 38.322 }, { "datetime": "2012-04-11 06:38:37", "depth": 22.9, "lng": 93.0632, "src": "us", "eqid": "c000905e", "magnitude": 8.6, "lat": 2.311 }, { "datetime": "2007-09-12 09:10:26", "depth": 30, "lng": 101.3815, "src": "us", "eqid": "2007hear", "magnitude": 8.4, "lat": -4.5172 }]}

请推荐教程或发布一些代码,它真的对我有帮助。 提前致谢

【问题讨论】:

    标签: ionic-framework hybrid-mobile-app multi-device-hybrid-apps


    【解决方案1】:

    首先,您需要数据提供者earthquakes-provider.ts

    import { Injectable } from '@angular/core';
    import { Http } from '@angular/http';
    import 'rxjs/add/operator/map';
    
    @Injectable()
    export class EarthquakesProvider {
    
      constructor(public _http: Http) {
        console.log('Hello Earthquakes Provider');
      }
    
      loadEarthquakes(){
        return this._http.get('http://api.geonames.org/earthquakesJSON?north=44.1&south=-9.9&east=-22.4&west=55.2&username=bertt')
          .map(res => res.json());
      }
    
    }
    

    其次,您需要在 home.ts

    中显示 JSON 数据的页面
    import { Component } from '@angular/core';
    import { NavController } from 'ionic-angular';
    import { EarthquakesProvider } from '../../providers/earthquakes-provider';
    
    @Component({
      selector: 'page-home',
      templateUrl: 'home.html',
      providers: [EarthquakesProvider]
    })
    export class HomePage {
    
      public DateList: Array<Object>;
    
      constructor(public _navCtrl: NavController,
                  public _earthquakes: EarthquakesProvider ) {
    
           this.getEarthquakes();
    
      }
    
      getEarthquakes(){
         this._earthquakes.loadEarthquakes().subscribe(res => {
         this.DateList = res.earthquakes;
         console.log(res.earthquakes);
        });
      }
    
    }
    

    最后是 home.html

    <ion-header>
      <ion-navbar>
        <button ion-button menuToggle>
          <ion-icon name="menu"></ion-icon>
        </button>
        <ion-title>Show dates in List</ion-title>
      </ion-navbar>
    </ion-header>
    
    <ion-content class="content-background">
    
    <ion-list>
      <button ion-item *ngFor="let item of DateList">
        {{ item.datetime }}
      </button>  
    </ion-list>
    
    </ion-content>
    

    附:您可以使用MomentJS 来解析、验证、操作和显示日期和时间

    【讨论】:

    • 先生,我收到此错误“错误:没有 Http 提供程序!” @Tomislav Stankovic
    猜你喜欢
    • 2017-12-11
    • 2019-09-29
    • 2016-05-17
    • 2020-02-19
    • 2017-04-10
    • 2019-02-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多