简体   繁体   English

如何使用jQuery追加包含角度指令的元素

[英]How to append an element containing an angular directive using jquery

I am working on a web app using angular meteor and writing JS in ECMAscript 6 class style. 我正在使用角度流星开发Web应用程序,并以ECMAscript 6类样式编写JS。 I need to append an element, that will later be clicked on, to an existing div dynamically. 我需要将一个元素(以后将被单击)动态地添加到现有的div上。

The problem I am having is that when I append the element, the angular ng-mouseover directive contained within the appended element's html disappears and does not work. 我遇到的问题是,当我附加元素时,包含在附加元素的html中的有角ng-mouseover指令会消失并且不起作用。

I want the newly appended element to listen for hover events, and the only solution I have found for doing so is to somehow use $compile . 我希望新添加的元素侦听悬停事件,而我发现的唯一解决方案是以某种方式使用$compile However, I am extremely new to Angular and struggled to understand the $compile documentation, especially when trying to convert it into ECMAscript acceptable syntax. 但是,我对Angular还是陌生的,并且很难理解$compile文档,尤其是在尝试将其转换为ECMAscript可接受的语法时。 Can anyone help me do this correctly? 谁能帮我正确地做到这一点?

My attempt at appending the element via JQuery: 我尝试通过JQuery追加元素:

$('#progress-container').append("<img src='greenpin.png' ng-click='chat.hovering($event)' id='" + pinId + "' class='pin' style='left: " + pos + "%'/>");

Using $compile would do the trick! 使用$ compile可以解决问题!

$('body').append($compile("<my-angular-directive />")(scope));
scope.$apply();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM