gpt4 book ai didi

javascript - Angularjs 在指令中获取输出后将值分配给 Controller

转载 作者:行者123 更新时间:2023-12-02 16:17:41 25 4
gpt4 key购买 nike

我对 angularjs 和 googlemaps 很陌生,请帮助我。

我的 html 文件:

<body ng-app="myApp" ng-controller="carLocationCtrl">

<div my-map=""></div>
<p>{{myCurrentPosition}}</p>
<script type="text/javascript" src="E:\Projects\Angular Projects\angularmap.js"></script>

</body>

在这里,我想从指令获取 currentPosition 的值,然后将该值传递或分配给我的 Controller 变量 myCurrentLocation。然后只有我才能在我的 <p> 中显示该值。标签。

我的 Controller :

    var OEMControllers=angular.module('myApp',[]);

OEMControllers.controller('carLocationCtrl', ["$scope", "$rootScope", function ($scope, $rootScope) {
$scope.isEditLocation = false;
$scope.myCurrentLocation='';
$scope.saveCarLocation = function () {
$scope.isEditLocation = true;
};
$scope.editCarLocation = function () {
$scope.isEditLocation = false;
};
}]).directive('myMap', function() {

var link = function(scope, element, attrs) {
var map, infoWindow;
var markers = [];

// map config
var mapOptions = {
center: new google.maps.LatLng(50, 2),
zoom: 4,
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) {
var marker;
var markerOptions = {
position: position,
center:position,
map: map,
icon: 'https://maps.google.com/mapfiles/ms/icons/green-dot.png'
};

marker = new google.maps.Marker(markerOptions);
markers.push(marker); // add marker to array

//pan to marker..
console.log('marker.getPosition() :: '+marker.getPosition());
map.panTo(marker.getPosition());
map.setZoom(16);
console.log('pan called');

//here i can able to get my currentPosition but how can i send this value to my controller .........

scope.myCurrentLocation=marker.getPosition();

console.log('output :: '+scope.myCurrentLocation);

}

// show the map and place some markers
initMap();
getLocation();

var watchID=null;
function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(onSuccess);

/* var options = { timeout: 30000 };
watchID = navigator.geolocation.watchPosition(onSuccess, onError, options);*/

} else {
console.log('Not obtain geolocation');
}
}

//geolocation succes..

function onSuccess(position) {

console.log('lat :: '+position.coords.latitude);
console.log('long :: '+position.coords.longitude);


setMarker(map, new google.maps.LatLng(position.coords.latitude, position.coords.longitude));
}

function onError(error)
{
console.log(error.message);
}

};

return {
restrict: 'A',
template: '<div id="gmaps"></div>',
replace: true,
link: link
};
});

最佳答案

我认为你的代码没问题,但在你的html中你使用了错误的变量名称,如“myCurrentPosition”,我认为它可以与“myCurrentLocation”一起使用

关于javascript - Angularjs 在指令中获取输出后将值分配给 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29429827/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com