绑定外部加载DOM元素角的范围 [英] Bind an externally loaded DOM element to Angular's scope

查看:98
本文介绍了绑定外部加载DOM元素角的范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是角应用程序中谷歌地图。根据谷歌地图的API,我通过字符串 google.maps 函数包含HTML和库显示它地图作为InfoView中上。

I'm using Google Maps within an Angular app. As per Google Maps's API, I pass a string to a google.maps function containing HTML, and the library displays it on the map as an InfoView.

我加入一个按钮,这个观点,我想这个观点在我的角度应用程序绑定到一个控制器,但是,即使我通过< D​​IV NG控制器=myController的> ...< / DIV> ,当地图库其附加到DOM,角度不通知

I'm adding a button to this view, and I want this view to be bound to a controller in my Angular app, however, even if I pass <div ng-controller="MyController">...</div>, when the Maps library attaches it to the DOM, Angular is not notified.

我怎么能强迫该元素的汇编(上,我很少有控制:刚好路过一个字符串)?

How can I force the compilation of this element (over which I have very little control: just passing a string)?

推荐答案

有关AngularJS通知的指令,它会需要编译的元素,这是不是自举应用程序时,除了自动完成。所以,无论是API需要让你的DOM 元素传递给它(你需要 $编译并链接您传递之前),或者你需要找到的元素,它被添加到DOM,然后在 $编译并链接。据我所知,如果你不能做任何的那些,那么你要问是不可能的。

For AngularJS to "notice" the directive, it'd need to compile the element, which is not done automatically except when bootstrapping the application. So either the API needs to allow you to pass DOM Element to it (and you need to $compile and link it before you pass it on), or you need to find the element after it is added to DOM and then $compile and link it. AFAIK, if you cannot do either of those, then what you're asking is impossible.

如果您设法得到元素的保留,编译它是就这么简单调用

If you manage to get hold of the element, compiling it is as simple as calling

$compile(element)($scope);

其中, $范围是你要链接到(甚至可能是 $ rootScope )的范围。

where $scope is the scope you want to link it to (possibly even $rootScope).

这篇关于绑定外部加载DOM元素角的范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆