Passing ng-directive to Leaflet L.popup().setContent()

隐身守侯 提交于 2019-12-08 06:40:50

问题


Thank you for giving my question a look.

Goal: Pass an Angular directive to the .setContent() method of L.popup()

The Problem:

I need to run $compile on the directive in order for it to enter ng. But something like

.setContent($compile('<new_marker_form></new_marker_form'))

yields a

Failed to execute 'appendChild' on 'Node': The new child element is null.

as I bet the ng is trying to compile before leaflet has actually appeneded any HTML.

Not sure if this is better suited for Stack Overflow. Please let me know if I ought to move it.


回答1:


The closing > is missing in your example, not sure if it is just a typo in the question.

And you haven't linked the compiled element to any scope, the result of $compile is not an element, but a linking function. You have to call the function by passing a scope object that you want the element to bind with (or a least an empty object).

var linkFn = $compile('<new_marker_form></new_marker_form>');
var element = linkFn({}); // or pass any scope object, may be $rootScope.$new() if you do not have one.
L.popup().setContent(element[0]); // use element[0] to pass a DOM instead of jQuery/jqLite object.

Or a one liner ..

L.popup().setContent($compile('<new_marker_form></new_marker_form>')({}));

Hope this helps.



来源:https://stackoverflow.com/questions/25299128/passing-ng-directive-to-leaflet-l-popup-setcontent

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!