我通过google api获取数据,并在父组件中发送此数据。但是teplate父组件中的数据不会更新。请帮忙。
app.html
<app-google-autocomplete (coordinates)="getWeather($event)"></app-google-autocomplete>
<button type="button" (click)="reload()" *ngIf="city">reload</button>
<div *ngIf="city">
{{ city.name }}
</div>
app.component.ts
import {Component} from '@angular/core';
import {WeatherService} from '../../services/weather.service';
import {Weather} from '../../models/weather';
import {City} from '../../models/city';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
weather: Weather;
city: City;
constructor(
protected weatherService: WeatherService,
) {
}
getWeather(data): void {
this.city = data;
this.weatherService
.getByCoord(data.lat, data.lon)
.subscribe((weather) => {
this.weather = weather.main;
});
}
reload(): void {
this.getWeather(this.city);
}
}
google-autocomplete.component.html
<input id="pac-input" class="controls" type="text" placeholder="Search Box" #pac_input>
app-google-autocomplete.ts
/// <reference types="@types/googlemaps" />
import {Component, EventEmitter, OnInit, Output, ViewChild} from '@angular/core';
@Component({
selector: 'app-google-autocomplete',
templateUrl: './google-autocomplete.component.html',
styleUrls: ['./google-autocomplete.component.css']
})
export class GoogleAutocompleteComponent implements OnInit {
@ViewChild('pac_input') searchElement;
@Output() coordinates: EventEmitter<any> = new EventEmitter<any>();
public searchBox: google.maps.places.SearchBox;
public places;
constructor() {
}
ngOnInit() {
this.searchBox = new google.maps.places.SearchBox((
this.searchElement.nativeElement
));
this.searchBox.addListener('places_changed', (event) => {
this.places = this.searchBox.getPlaces();
if (this.places[0]) {
this.coordinates.emit({
lat: this.places[0].geometry.location.lat(),
lon: this.places[0].geometry.location.lng(),
name: this.places[0].name
});
}
});
}
}
亲爱的同事们,如果您能帮助我,我会很高兴。我不知道我做错了什么。 我在py项目中使用了Angular 6。