所以我尝试为leaflet.js创建指令,当我使用factory inside指令时,一切正常
(function() {
'use strict';
angular
.module('directoryAppMap')
.directive('leafletDirective', function (Directory) {
return {
restrict: 'EA',
template:'<div></div>',
link: function (scope,element, attrs) {
var map = L.map(attrs.id, {
center: [40, -86],
zoom: 2
});
//create a CloudMade tile layer and add it to the map
L.tileLayer('http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
attribution: '© <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a>',
maxZoom: 18
}).addTo(map);
Directory.get(function (data) {
L.geoJson(data).addTo(map);
});
}
};
});
})();
在我的控制器中,我会做一些事情并将其传递给视图
$scope.geojson = {};
$scope.FilteredGeojson = function () {
var result;
result = $filter('filter')($scope.data, $scope.search);
result = $filter('offset')(result, $scope.currentPage * $scope.pageSize);
result = $filter('limitTo')(result, $scope.pageSize);
$scope.geojson = result;
return result;
};
表中的一切正常我使用ng-repeat和FilteredGeojson()但是当我尝试将$ scope.geojson传递给指令时,angular start无限的摘要循环和map没有任何geojson
以前的指令我使用add
scope: {
data: '='
}
在视图中我传递
data="geojson"
不幸的是,我无法使用leaflet指令进行角度调整,因为对于很多标记来说非常慢
答案 0 :(得分:1)
尝试时是否拆除了工厂?这对我有用:
指令:
angular.module('app').directive('leaflet', [
function () {
return {
restrict: 'EA',
replace: true,
scope: {
data: "="
},
template: '<div></div>',
link: function (scope, element, attributes) {
var map = L.map(element[0], {
center: [0, 0],
zoom: 0
});
var tileLayer = L.tileLayer('http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
attribution: '© <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a>',
maxZoom: 18
}).addTo(map);
var geojsonLayer = L.geoJson(scope.data).addTo(map);
map.fitBounds(geojsonLayer.getBounds());
}
};
}
]);
控制器:
angular.module('app').controller('rootController', [
'$scope',
function ($scope) {
$scope.geojson = {
"type": "FeatureCollection",
"features": [{
"type": "Feature",
"properties": {},
"geometry": {
"type": "Point",
"coordinates": [45, 45]
}
},{
"type": "Feature",
"properties": {},
"geometry": {
"type": "Point",
"coordinates": [-45, -45]
}
}]
};
}
]);
模板:
<leaflet data="geojson"></leaflet>
以下是关于Plunker的工作示例:http://plnkr.co/edit/0cUudGJp2VwRtxFBMRqc?p=preview
根据下面评论中的请求,另一种实现方法,实际上它是一种与传单指令完全不同的方法。保持控制器中的所有逻辑。回调方法:
指令:
angular.module('app').directive('leaflet', [
function () {
return {
restrict: 'EA',
replace: true,
scope: {
callback: "="
},
template: '<div></div>',
link: function (scope, element, attributes) {
scope.callback(L.map(element[0]));
}
};
}
]);
模板:
<leaflet callback="callback"></leaflet>
控制器:
angular.module('app').controller('rootController', [
'$scope',
function ($scope) {
$scope.geojson = {
// See controller above
};
$scope.callback = function (map) {
var tileLayer = L.tileLayer('http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
attribution: '© <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a>',
maxZoom: 18
}).addTo(map);
var layer = L.geoJson($scope.geojson).addTo(map);
map.fitBounds(layer.getBounds());
};
}
]);
以下是此方法的一个有效示例:http://plnkr.co/edit/0cUudGJp2VwRtxFBMRqc?p=preview