Angularjs,无法将数据传递给 geojsonLayer Leaflet
Angularjs, cannot pass data to geojsonLayer Leaflet
所以我尝试为 leaflet.js 创建指令,当我使用工厂内部指令时,一切正常
(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;
};
在 table 中一切正常我使用 ng-repeat 和 FilteredGeojson() 但是当我尝试将 $scope.geojson 传递给指令时,angular 开始无限摘要循环并且映射是没有任何 geojson
我使用 add
对之前的指令
scope: {
data: '='
}
看来我通过了
data="geojson"
不幸的是我不能为 angular 使用 leaflet 指令,因为有很多标记非常慢
您尝试时是否删除了工厂?这对我有用:
指令:
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
所以我尝试为 leaflet.js 创建指令,当我使用工厂内部指令时,一切正常
(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;
};
在 table 中一切正常我使用 ng-repeat 和 FilteredGeojson() 但是当我尝试将 $scope.geojson 传递给指令时,angular 开始无限摘要循环并且映射是没有任何 geojson
我使用 add
对之前的指令 scope: {
data: '='
}
看来我通过了
data="geojson"
不幸的是我不能为 angular 使用 leaflet 指令,因为有很多标记非常慢
您尝试时是否删除了工厂?这对我有用:
指令:
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