Directive / *ng如果只调用一次,启用在地图上重绘新方向
Directive / *ngIf only called one time, enable to redraw new direction on map
首先,这是我第一次使用 angular,我通过这个迷你项目来熟悉这个框架。
在这个项目中,我有一个起点和终点坐标,以及 waypoints 坐标。我希望 google 地图使用 waypoints 给出从起点到终点的方向。
我正在使用 Angular Google 地图:https://github.com/SebastianM/angular-google-maps ,因为它不支持地图方向 我创建了一个指令,它将在地图上绘制方向
这是 plnkr 中的一些代码:https://plnkr.co/edit/JtZrBJHNufUFWnVjwcxX
<agm-map [latitude]="lat" [longitude]="lng">
<!--<agm-marker-->
<!--*ngFor="let m of markers; let i=index"-->
<!--[latitude]="m.lat"-->
<!--[longitude]="m.lng"-->
<!--></agm-marker>-->
<appAgmMapDirections
*ngIf="drawRoute == true"
[origin]="origin"
[destination]="destination"
[waypoints]="wayPoints"
[directionsDisplay]="directionsDisplay"
></appAgmMapDirections>
我遇到的问题是方向只会被绘制一次,它不会移除旧方向并绘制新方向。以前可以,现在不行了
我注意到指令只调用了一次
*ngIf="drawRoute"
即使 drawRoute 值已更改。
这可能是什么问题?
提前谢谢你。
您在同一更改检测周期内将 drawRoute
设置为 false
和 true
。解决方法是将其设置为 false,然后使用 setTimeout
强制另一个更改检测周期为 运行:
setTimeout(() => {
this.drawRoute = true;
}, 100);
首先,这是我第一次使用 angular,我通过这个迷你项目来熟悉这个框架。
在这个项目中,我有一个起点和终点坐标,以及 waypoints 坐标。我希望 google 地图使用 waypoints 给出从起点到终点的方向。
我正在使用 Angular Google 地图:https://github.com/SebastianM/angular-google-maps ,因为它不支持地图方向 我创建了一个指令,它将在地图上绘制方向 这是 plnkr 中的一些代码:https://plnkr.co/edit/JtZrBJHNufUFWnVjwcxX
<agm-map [latitude]="lat" [longitude]="lng">
<!--<agm-marker-->
<!--*ngFor="let m of markers; let i=index"-->
<!--[latitude]="m.lat"-->
<!--[longitude]="m.lng"-->
<!--></agm-marker>-->
<appAgmMapDirections
*ngIf="drawRoute == true"
[origin]="origin"
[destination]="destination"
[waypoints]="wayPoints"
[directionsDisplay]="directionsDisplay"
></appAgmMapDirections>
我遇到的问题是方向只会被绘制一次,它不会移除旧方向并绘制新方向。以前可以,现在不行了
我注意到指令只调用了一次
*ngIf="drawRoute"
即使 drawRoute 值已更改。
这可能是什么问题? 提前谢谢你。
您在同一更改检测周期内将 drawRoute
设置为 false
和 true
。解决方法是将其设置为 false,然后使用 setTimeout
强制另一个更改检测周期为 运行:
setTimeout(() => {
this.drawRoute = true;
}, 100);