Angular2:如何访问使用Http.get检索的对象数组

时间:2017-01-18 22:31:52

标签: angular typescript

这是我的代码:

import { Component, ViewChild, ElementRef } from '@angular/core';
import { NavController } from 'ionic-angular';
import { EventService } from '../../app/event.service';

declare var google;

@Component({
  selector: 'map-view',
  templateUrl: 'mapview.html'
})
export class MapView
{

  @ViewChild('map') mapElement: ElementRef;
  map: any;
  events: any;

  constructor(public navCtrl: NavController, private eventService: EventService)
  {

  }

  ngOnInit()
  {
    this.getEvents();
    this.loadMap();
  }

  loadMap()
  {
    let center = new google.maps.LatLng(-34.9290, 138.6010);
    let mapOptions =
    {
      zoom: 13,
      center: center,
      mapTypeControl: false,
      streetViewControl: false,
    }
    this.map = new google.maps.Map(this.mapElement.nativeElement, mapOptions);
  }

  getEvents()
  {

    this.eventService.getEvents().subscribe(response =>
    {
      this.events = response.lexington;
      console.log(this.events[0]);
    })


  }
}

基本上,现在正在做的是调用从URL检索json对象的服务。我知道那部分工作正常。但是,如何将该响应提供给文件的其余部分?

1 个答案:

答案 0 :(得分:0)

您只是想保存回复吗?

修改

根据评论,您似乎希望使用响应来填充Google地图。

所以只需在获取事件json对象

之后再做

像:

  @ViewChild('map') mapElement: ElementRef;
  map: any;
  events: any;
  eventsReponse:any; // this

  getEvents()
  {

    this.eventService.getEvents().subscribe(response =>
    {
      this.eventsReponse = response;  

      // You have the response here, you can populate google maps 
      // here like

      this.populateGoogleMaps(response);
      ...
    })


 }