title: leaflet - Looking for method of adding onEachFeature to existing geojson layer
body: <p>I'm working with geojson data in leaflet. In their guide at <a href="http://leafletjs.com/examples/geojson.html" rel="noreferrer">http://leafletjs.com/examples/geojson.html</a> they write that there are two ways of adding geojson data to the map:</p>
  
  <p><em>"GeoJSON objects are added to the map through a GeoJSON layer. To create it and add it to a map, we can use the following code:"</em></p>
  
  <pre><code>L.geoJson(geojsonFeature).addTo(map);
  </code></pre>
  
  <p><em>"Alternatively, we could create an empty GeoJSON layer and assign it to a variable so that we can add more features to it later."</em></p>
  
  <pre><code>var myLayer = L.geoJson().addTo(map);
  myLayer.addData(geojsonFeature);
  </code></pre>
  
  <p>I am currently using the last method (I'm creating an empty layer and adds data to that). What I am doing right now is that I'm trying to add an onEachFeature function. Though I'm not getting it to work when using the myLayer.addData way of doing it.</p>
  
  <p>In my code I'm using <code>this.geoJson.addData(geoJson, { onEachFeature: onEachFeature });</code>. Where geoJson is the actual geoJson string and "this.geoJson" is the empty geoJson layer that's been created and added to the map. Though this is not working. </p>
  
  <p>But if I create a new geoJson layer at the same time I'm entering the geoJson data it all works:</p>
  
  <pre><code>L.geoJson(geoJson, {
      onEachFeature: onEachFeature
  }).addTo(this.map);
  </code></pre>
  
  <p>So what I'm mainly wondering is how I should do to make the onEachFeature function work when adding the data with the layer.addData function?</p>