简体   繁体   English

从普通JS调用angular函数

[英]Call angular function from plain JS

I'm trying to integrate Fastsprings JS library into my Angular.js application. 我正在尝试将Fastsprings JS库集成到我的Angular.js应用程序中。 I have to embed Fastsprings library into my HTML head like: 我必须将Fastsprings库嵌入到HTML头中,例如:

<script id="fsc-api" src="..." data-popup-closed="onPopupClose" />

I've created a directive that uses document.createElement to build this script tag and add it to the document. 我创建了一个指令,该指令使用document.createElement来构建此脚本标记并将其添加到文档中。 My problem is that data-popup-closed contains the name of a callback function that is called if the fastspring popup gets closed. 我的问题是data-popup-closed包含一个回调函数的名称,如果fastspring弹出窗口被关闭,则该回调函数将被调用。 I tried to create this function inside my directive but that doesn't work because it only exists inside Angular scope then. 我尝试在指令中创建此函数,但此方法不起作用,因为那时它仅存在于Angular范围内。

What is the best way to handle such situations? 处理此类情况的最佳方法是什么? This is not a Fastspring specific question, this problem should always occur if you want to integrate a library which awaits a plain JS callback function. 这不是Fastspring特有的问题,如果您要集成一个等待普通JS回调函数的库,则应该总是发生此问题。

Can you try to propagate "onPopupClose" function outside of the angulars scope? 您可以尝试在角度范围之外传播“ onPopupClose”功能吗? Do something like: window. 做类似的事情:窗口。 onPopupClose = $scope. onPopupClose = $ scope。 onPopupClose; onPopupClose; in your angular app and then you can use this function outside.. just call window.onPopupClose() 在您的角度应用程序中,然后您可以在外部使用此功能。.只需调用window.onPopupClose()

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

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