如果在Leaflet中没有填充文本属性,如何在弹出窗口中隐藏它?

时间:2015-03-23 12:57:02

标签: javascript leaflet geojson

我有geoJson图层:

var test_layer = {
    "type": "FeatureCollection",
    "crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:OGC:1.3:CRS84" } },                                                                         
    "features": [{ 
        "type": "Feature", 
        "properties": { 
            "name": "Name 1",
            "description": "Desc 1", 
        "geometry": { 
        "type": "Point", 
        "coordinates": [ 
        72.6, 44.3]
        }
        }]
    }

和弹出功能:

onEachFeature: function (feature, layer) {
        var popupContent = "<div class=popup><b>Object's name: </b>" + feature.properties.name + "<br /><b>Object's description: </b>" + feature.properties.description</p>";
        layer.bindPopup(popupContent);
            }

并非所有对象都有描述。如果我点击没有描述的点,在弹出窗口中我可以看到:

Description: undefined

当值为空或删除时,我应该更改什么才能在弹出窗口中隐藏描述字段?

1 个答案:

答案 0 :(得分:1)

试试这个:

 onEachFeature: function (feature, layer) {    
      var popupContent = "<div class=popup><b>Object's name: </b>" + feature.properties.name;
      if(typeof(feature.properties.description) !== 'undefined'){
          popupContent += "<br /><b>Object's description: </b>" + feature.properties.description+"</p>"
      }

        layer.bindPopup(popupContent);
      }