javascript-角度指令未更新google map值
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了javascript-角度指令未更新google map值,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3278字,纯文字阅读大概需要5分钟。
内容图文
![javascript-角度指令未更新google map值](/upload/InfoBanner/zyjiaocheng/665/15a78cce6a7f47bcbd25cf58960b1f78.jpg)
我有一个名为“ myMap”的指令,用于添加Google地图.
当我尝试使用控制器功能再次更新经度和纬度的其他位置时,它没有更新指令值.显示相同的地图.
这是我的指令:
directive('myMap', function () {
// directive link function
var link = function (scope, element, attrs) {
var map, infoWindow;
var markers = [];
// map config
var mapOptions = {
center: new google.maps.LatLng(attrs.latitude, attrs.longitude),
zoom: 12,
mapTypeId: google.maps.MapTypeId.ROADMAP,
scrollwheel: false
};
// init the map
function initMap() {
if (map === void 0) {
map = new google.maps.Map(element[0], mapOptions);
}
}
// place a marker
function setMarker(map, position, title, content) {
var marker;
var markerOptions = {
position: position,
map: map,
title: title,
icon: 'https://maps.google.com/mapfiles/ms/icons/green-dot.png',
animation: google.maps.Animation.Bounce
};
marker = new google.maps.Marker(markerOptions);
markers.push(marker); // add marker to array
google.maps.event.addListener(marker, 'click', function () {
// close window if not undefined
if (infoWindow !== void 0) {
infoWindow.close();
}
// create new window
var infoWindowOptions = {
content: content
};
infoWindow = new google.maps.InfoWindow(infoWindowOptions);
infoWindow.open(map, marker);
});
}
// show the map and place some markers
initMap();
setMarker(map, new google.maps.LatLng(attrs.latitude, attrs.longitude), attrs.restname, attrs.address);
};
return {
restrict: 'A',
template: '<div id="gmaps"></div>',
replace: true,
link: link
};
});
之后,我从HTML调用此指令.这是我的HTML:
<div class="gmaps" my-map="" latitude="{{home.latitude}}" longitude="{{home.longitude}}"></div>
我坚持这一点.我使用了不同的方法,但在我的情况下不起作用.
如何检查指令参数的不同变化,以便我可以分析经度和纬度的更新值?
请帮我吗?
解决方法:
在您的指令中添加观察者.
attrs.$observe("value", function (data) {},true);
它观察指令参数的变化,并在更改参数时对其进行更新.
在你的情况下
lahorefoodsWebSiteModule.directive('myMap', function () {
// directive link function
var link = function (scope, element, attrs) {
attrs.$observe("latitude", function (latitude) {
//This gets called when data changes.
var map, infoWindow;
var markers = [];
// map config
var mapOptions = {
center: new google.maps.LatLng(attrs.latitude, attrs.longitude),
zoom: 12,
mapTypeId: google.maps.MapTypeId.ROADMAP,
scrollwheel: false
};
// init the map
function initMap() {
if (map === void 0) {
map = new google.maps.Map(element[0], mapOptions);
}
}
// place a marker
function setMarker(map, position, title, content) {
var marker;
var markerOptions = {
position: position,
map: map,
title: title,
icon: 'https://maps.google.com/mapfiles/ms/icons/green-dot.png',
animation: google.maps.Animation.Bounce
};
marker = new google.maps.Marker(markerOptions);
markers.push(marker); // add marker to array
google.maps.event.addListener(marker, 'click', function () {
// close window if not undefined
if (infoWindow !== void 0) {
infoWindow.close();
}
// create new window
var infoWindowOptions = {
content: content
};
infoWindow = new google.maps.InfoWindow(infoWindowOptions);
infoWindow.open(map, marker);
});
}
// show the map and place some markers
initMap();
setMarker(map, new google.maps.LatLng(attrs.latitude, attrs.longitude), attrs.restname, attrs.address);
},true);
};
return {
restrict: 'A',
template: '<div id="gmaps"></div>',
replace: true,
link: link
};
});
HTML代码将相同.希望它能解决您的问题.
内容总结
以上是互联网集市为您收集整理的javascript-角度指令未更新google map值全部内容,希望文章能够帮你解决javascript-角度指令未更新google map值所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。