【发布时间】:2016-06-04 06:17:48
【问题描述】:
我正在使用 Google 的地点 api - getPlacePredictions。
这是我的 input keyup 事件的代码:
我的模板:
<input type="text" (keyup)='search()'>
<div *ngIf="searching">Searching ... </div>
我的班级:
private autocomplete;
ngAfterViewInit () {
this.autocomplete = new google.maps.places.AutocompleteService();
}
search(){
this.searching = true;
this
.autocomplete
.getPlacePredictions( option , ( places , m )=> {
console.log( 'onPrediction' , places );
this.searching = false;
} );
}
是否有任何可行的方法将 getPlacePredictions 包装在 rxjs 可观察对象中,以便我可以利用订阅此函数的优势?
我最终在这里要做的是创建一个可见和不可见的加载图标,但我无法用谷歌的 api 本身正确地做到这一点,我想如果我可以将它包装在一个 Observable 中,它会变成容易。
【问题讨论】:
-
见angular.io/docs/ts/latest/cookbook/…。它解释了如何在服务中使用 RxJS5 主题(它比 @Jigar 呈现的更 Angular)。将您的 Google 地图交互放入服务中,并使用
next()发出数据。让组件订阅更改。
标签: angular rxjs observable google-places