javascript - 如何使用来自 Google Maps DirectionsService.route() 函数的 Angular 双向绑定?
问题描述
这是我的 Angular 组件
export class MapsComponent implements OnInit {
@ViewChild('googleMap') gmapElement: any;
map: google.maps.Map;
data = "initialised";
ngOnInit() {
var directionsService = new google.maps.DirectionsService;
var directionsDisplay = new google.maps.DirectionsRenderer;
var map = new google.maps.Map(this.gmapElement.nativeElement, {
zoom: 7,
center: {lat: 41.85, lng: -87.65}
});
directionsDisplay.setMap(map);
directionsService.route({
origin: "terrell hills, tx",
destination: "alamo heights, tx",
travelMode: google.maps.TravelMode.DRIVING
}, (response, status) => {
if (String(status) === 'OK') {
directionsDisplay.setDirections(response);
this.data = "I'm modified in directionsService";
/***********************************************
here some string is assigned to this.data, but it was not updated in the "data" member of this class. The value of the member "data" is always showing "initialised" in the HTML template.
***********************************************/
} else {
alert('Directions request failed due to ' + status);
}
});
}
这是我的模板
<span>{{data}}</span> <!-------------here the the data is always showing "initialised"-------------->
有人可以告诉我是什么问题。
alert(this.data)
行前this.data = "I'm modified in directionsService";
的值为初始化alert(this.data)
行后this.data = "I'm modified in directionsService";
的值为我在directionsService中修改的- 但主要班级成员
data
没有更新。 - 我也尝试过创建另一个函数
function testFunc(x:any) { this.data = x; }
现在调用这个函数this.testFunc("some text")
是ngOnInit()
成功更新data
但是调用函数this.testFunc("some text inside directionsService.route")
是directionsService.route
更新值data
- 我也在
var self = this
函数之外尝试过,self.data = "some text with self"
也没有工作。
有人可以帮我吗?提前谢谢了。
解决方案
未检测到更改,因为它发生在 google 方向回调中的角度框架之外。你有几个选择。我将按照从概念上最简单到最难的顺序列出它们。
力变化检测
constructor(private ref: ChangeDetectorRef){}
....
if (String(status) === 'OK') {
directionsDisplay.setDirections(response);
this.data = "I'm modified in directionsService";
this.ref.detectChanges();
...
在 Angular2 区域中运行
constructor(private ngZone:NgZone) {}
...
this.ngZone.run(() => {
-- Run directions query here
});
...
将方向调用包装在可观察对象中
const directionsObservable = Observbable.create(observer => {
directionsService.route({
origin: "terrell hills, tx",
destination: "alamo heights, tx",
travelMode: google.maps.TravelMode.DRIVING
}, (response, status) => {
if (String(status) === 'OK') {
directionsDisplay.setDirections(response);
observer.next("I'm modified in directionsService");
} else {
alert('Directions request failed due to ' + status);
}
});
directionsObservable.subscribe(text => this.data = text);
我使用了一个非常简单的示例来说明可观察对象的工作方式。您可能应该将方向调用提取到单独的服务,并在那里使用 observable 方法。
推荐阅读
- git - 如何在 Visual Studio 2017 中拉/合并请求?
- jquery - 免费 - jqgrid:结合冻结列、分组标题和工具栏搜索
- react-native - 如何在本机反应中从屏幕上删除顶部选项卡?
- php - 缺少 mysqli 扩展。请检查您在 php7.2.10 中的 PHP 配置错误
- android - 更改 WebView 中的字体系列
- java - Android-Firebase 将数据检索到列表视图中,但它不可见
- ruby-on-rails - rails中的`new`和`build`有什么区别?
- java - 插入地图时按值保留顺序
- javascript - 存储用户输入和显示
- angular - 无法在 Angular 表单中使用 patchValue 方法