我正在尝试使用Angular-Leaflet-Directive 和来自 2 个使用 $resource 调用 Web 服务的不同 Angular 服务的数据来生成地图。这些服务返回包含纬度/经度值的 JSON 消息以填充地图上的标记。
但是,我无法获得使用服务数据的指令。
我可以看到我的服务正在更新范围,因此它们工作正常,并且如果我将值硬编码到控制器中(例如,请参见下面的“中心”),地图标记会正确呈现。我想做的和这个例子很相似
这是我的代码:
控制器:
angular.module('myDetails', ['leaflet-directive', 'shop', 'home'])
.controller('MyDetailsCtrl', ['$scope', '$routeParams', '$http', '$q', '$rootScope', 'shopService', 'homeService', function ($scope, $routeParams, $http, $q, $rootScope, shopService, homeService) {
function getShop() {
var d = $q.defer();
var shop = shopService.get({shopId: $routeParams.shopId}, function (shop) {
d.resolve(shop);
});
return d.promise;
}
function getHome() {
var d = $q.defer();
var home = homeService.get({homeId: $routeParams.homeId}, function (home) {
d.resolve(home);
});
return d.promise;
}
$q.all([
getShop(),
getHome()
]).then(function (data) {
var shop = $scope.shop = data[0];
var home = $scope.home = data[1];
var markers = {
shop: {
lat: $scope.shop.loc.coordinates[0],
lng: $scope.shop.loc.coordinates[1],
draggable: false,
message: $scope.shop.name,
focus: true
},
home: {
lat: $scope.home.loc.coordinates[0],
lng: $scope.home.loc.coordinates[1],
draggable: false,
message: $scope.home.name,
focus: true
}
};
console.log("Markers are " + angular.toJson(markers));
$scope.markers = markers;
});
$scope.center = {
lat: 53.26,
lng: -2.45,
zoom: 6
};
}]);
我发现我的 2 个服务返回,范围用值更新,但这并没有传递给 angular-leaflet-directive。
关于指令的Angular 文档建议指令中的以下代码将子作用域链接到父作用域,以便它们都被更新:
scope: {
center: '=center',
maxBounds: '=maxbounds',
bounds: '=bounds',
marker: '=marker',
markers: '=markers',
defaults: '=defaults',
paths: '=paths',
tiles: '=tiles',
events: '=events'
}
但这似乎对我不起作用。但是,angular-leaflet-directive 路径示例似乎确实允许这样做(您可以添加标记、更改标记等)并且地图会实时更新。
服务返回后,我需要做什么才能使标记出现在我的地图上?
请注意,Stackoverflow 上有类似的问题,但这些问题的答案是将服务调用包含在指令中。我不想这样做,因为控制器提供的功能不仅仅是直接的服务调用,例如处理表单提交等,并且需要访问相同的数据。