我试图通过点击多边形来传递参数来绑定它,我有以下内容:
var mapLayer = new L.TopoJSON(jsonMap, {style: style, onEachFeature: onEachFeature.bind(null,null,selectionManager), pane:'borders'}).addTo(this.map);
function onEachFeature(feature, layer, selectionManager) {
console.log(selectionManager)
layer.on({
mouseover: highlightFeature,
mouseout: resetHighlight,
click: dataFilter.bind(null,ID)
});
}
function dataFilter(selectionManager,e){
var layer = e.target;
var zoneName = layer.feature.properties.Index;
console.log(zoneName)
console.log(selectionManager);
}
所以我的目标是读取dataFilter中的参数(在本例中为selectionManager)
答案 0 :(得分:3)
根据传单文档,onEachFeature
需要是一个接收两个参数(feature
和layer
)的函数。按照您使用它的方式使用Function.prototype.bind
并不能达到您想要的效果。
而是创建一个closure:
function onEachFeatureClosure(dataFilter) {
return function onEachFeature(feature, layer) {
// Your own logic, that uses dataFilter as well as feature and layer
}
}
L.topoJSON(jsonMap, {
style: style,
onEachFeature: onEachFeatureClosure(selectionManager),
pane:'borders'
}).addTo(this.map);
请注意,onEachFeatureClosure(selectionManager)
的返回值是一个类似function onEachFeature(feat, layer) {...}
的函数。