javascript - Angular 2插值在谷歌地图的监听器内不起作用
问题描述
我有一个使用谷歌地图的 Angular 2 项目。地图最初出现。移动地图时,我想获取地图的中心并使用地理编码来获取地址。得到地址后,我想在信息屏幕上显示地址。
@Component({
templateUrl: '<div #map></div>
<p>{{info}}</p>'
})
export class MapComponent implement OnInit {
info = 'initial'
showInfo = string => {
console.log(this.string);
this.info = string;
console.log(this.string);
}
...
}
我正在使用方法showInfo
来帮助我调试这个问题。在 ngOnInit 中,我正在初始化地图。有趣的是,如果我直接从 ngOnInit 调用显示信息,<p>{{info}}</p>
浏览器中的文本会发生变化
ngOnInit() {
... // let say I'm already initialize the map
this.showInfo('test this'); // WORKING
}
但是在我将一个听众放入我的地图后,它就停止了工作
ngOnInit() {
this.map.addListener('center_changed', () => {
const location = ... // let say I'm already get the location
this.showInfo(location); // NOT WORKING
});
}
突然角度 2 插值无法工作并且浏览器中的文本没有改变,console.log()
里面showInfo()
说变量info
已经改变但里面的文本<p>{{info}}</p>
没有改变。
解决方案
推荐阅读
- git - 如何编辑 git rebase 内容然后通过 shell 保存?
- python - 如何在遍历所有文档的同时逐个更新文档的特定字段?
- mysql - 非典型 My-SQL 查询的执行顺序
- r - R:有没有办法插入完成年份序列的行?
- pandas - 缺失值(NaN 值)与填充值的重叠图
- javascript - 学习 Javascript(木偶师)
- c++ - 字符串到 C++ 中的双重转换导致问题
- c - 如何从一个函数中的多个参数中获取值并将其打印在 c 的主函数中?
- java - 将外部文档(来自 xyz-javadoc.jar)复制到 JavaDocs
- perl - 是否可以使用 DBIx 类防止读取和/或设置字段值?