简体   繁体   English

从Angular控制器调用外部库中的函数

[英]Call function in external library from Angular controller

We have a script placed on our site that is used to generate pop-up's/modals. 我们在网站上放置了一个脚本,用于生成弹出窗口/模态。

The modal can be invoked by calling 模态可以通过调用

ModalObject.Triggeronclick(someModalId)

However, I cannot call this function from the Angular controller (and I need access to data and logics within the controller, to trigger the call). 但是,我无法从Angular控制器调用此函数(并且我需要访问控制器内的数据和逻辑以触发调用)。

I have tried calling 我试过打电话

window.ModalObject.Triggeronclick(someModalId) 

... but I get: ...但是我得到:

"TypeError: Cannot read property 'triggerOnclick' of undefined".

How can I call this function from my app controller? 如何从我的应用程序控制器调用此函数?

If you insist on using your ModalObject instead of an angular specific modal resource, Then you should create a factory/service that returns $window.ModalObject - $window being the angular injected window object. 如果您坚持使用ModalObject而不是特定于角度的模态资源,则应创建一个返回$window.ModalObject的工厂/服务-$ window是角度注入的窗口对象。 Then you can inject that factory/service into your controller. 然后,您可以将该工厂/服务注入到控制器中。

    app.factory('modalObject', ($window) => {
        return $window.ModalObject
    })

Assuming the ModalObject is available on the window object. 假设ModalObject在窗口对象上可用。

Then inject modalObject into your controller. 然后将modalObject注入您的控制器。

However, there are other options that are specifically angular modals: 但是,还有其他一些选项是角度模态:

You could try to add the resource to the index.html and access global variables with 您可以尝试将资源添加到index.html并使用以下命令访问全局变量

(<any>window).document

where you would normally just call 您通常会打电话给的地方

document

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

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