我正在尝试从当前位置获取经度和纬度,并将其设置为谷歌地图上的标记,但它无法正常工作。现在我无法访问创建的变量,我尝试在我的指令中执行导航器,但仍然没有任何工作。
JAXBContext jc = JAXBContext.newInstance(StackOverflowXPathMap.class);
Unmarshaller unmarshaller = jc.createUnmarshaller();
URL xmlURL = new URL("https://stackoverflow.com/feeds");
URLConnection conn = xmlURL.openConnection();
conn.addRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.86 Safari/537.36");
conn.connect();
InputStream xml = xmlURL.openStream();
StackOverflowXPathMap stackOverflowXPathMap = (StackOverflowXPathMap) unmarshaller.unmarshal(xml);
System.out.println(stackOverflowXPathMap.getAuthor());
xml.close();
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(stackOverflowXPathMap, System.out);
答案 0 :(得分:1)
尝试重新排序此行中注入的依赖项,以匹配参数顺序:
myApp.controller('PhoneListCtrl', ['$rootScope', '$http', '$scope', function ($scope, $http, $rootScope) {
固定行:
myApp.controller('PhoneListCtrl', ['$scope', '$http', '$rootScope', function ($scope, $http, $rootScope) {
同样在以下行中,$ scope未注入:
myApp.directive("myMaps", ['$rootScope', function($scope, $rootScope){
固定行:
myApp.directive("myMaps", ['$scope', '$rootScope', function($scope, $rootScope){
请记住,在这种情况下,数组中值的排序必须与控制器/指令中参数的顺序相匹配。
BTW,$ rootScope不建议不断变化的值。尝试使用指令范围。 https://docs.angularjs.org/guide/directive
答案 1 :(得分:0)
我不建议在这种情况下使用rootScope
。您需要等到angular评估变量,因此尝试控制日志指令定义中的var将不起作用。
您可以将控制器范围变量传递给指令。请参阅隔离范围的概念:将一些值从父范围传递到指令
AngularJS提供了3种类型的前缀:
为此,请在html中包含要传递给指令的范围变量。
<my-maps position="{{position}}"></my-maps>
只需像往常一样在控制器中使用$scope
。 (我在下面修改了依赖注入的顺序)
myApp.controller('PhoneListCtrl', ['$scope', '$http', function ($scope, $http) {
if(navigator.geolocation){
navigator.geolocation.getCurrentPosition(function(position){
$scope.position = position;
});
}
}]);
在指令
中添加范围定义scope: {
position: "="
}
完整指令:
myApp.directive("myMaps", ['$scope', function($scope){
return{
restrict:'E',
template:'<div></div>',
replace: true,
scope: {
position: "="
}
link: function(scope, element, attrs){
console.log(scope.position);
var myLatLng = new google.maps.LatLng(56, -75.732333);
var myLatLng2 = new google.maps.LatLng(56.2843691, -70.732333);
var mapOptions = {
center: myLatLng,
zoom: 5,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById(attrs.id), mapOptions);
var marker = new google.maps.Marker({
position: myLatLng,
map: map,
title: "hello world!"
});
var marker2 = new google.maps.Marker({
position: myLatLng2,
map: map,
title: "hello"
});
marker.setMap(map);
marker2.setMap(map);
}
};
} ]);