javascript Angular JS 和外部库

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26891972/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 06:39:14  来源:igfitidea点击:

Angular JS and external libraries

javascriptangularjs

提问by michelem

I have a third party JavaScript library (not Angular) and I would like to use its methods/objects from Angular.

我有一个第三方 JavaScript 库(不是 Angular),我想使用它来自 Angular 的方法/对象。

I know I can place a <script type="text/javascript">into an angular HTML view and use those methods there but that's really ugly.

我知道我可以将 a<script type="text/javascript">放入一个有角度的 HTML 视图并在那里使用这些方法,但这真的很难看。

How else could this be done?

这还能怎么做?

回答by michelem

Oh well, I found it, just attach to the $windowand you are done.

哦,我找到了,只需附加到$window就完成了。

As written here: https://developers.braintreepayments.com/javascript+node/sdk/client/setup

如此处所写:https: //developers.braintreepayments.com/javascript+node/sdk/client/setup

"The SDK will appear as braintree on the global window object."

SDK 将在全局窗口对象上显示为脑树。

So from your controller (for example) you can simply use $window.braintreeand you got everything you need from Braintree client library.

因此,从您的控制器(例如)中,您可以简单地使用$window.braintree并从 Braintree 客户端库中获得所需的一切。

To load the Dropin you can simply use this:

要加载 Dropin,您只需使用以下命令:

angular.module('app').controller('YourController', ['$scope', '$window',
    function ($scope, $window) {

        $window.braintree.setup('CLIENTTOKEN', 'dropin', {
            container: 'dropin'
        });

    }
]);

回答by SoluableNonagon

Include it before Angular and you should be fine

在 Angular 之前包含它,你应该没问题

<script type="text/javascript" src="braintree.js">
<script type="text/javascript" src="angular.js">

Using it with angular should not be a problem.

使用 angular 应该不成问题。

Can you create a Plunker or JSFiddle of a simple problem?

你能创建一个简单问题的 Plunker 或 JSFiddle 吗?

Sample answer of how to use the two together: Encrypting credit card details using AngularJS in Braintree

如何同时使用两者的示例答案:在 Braintree 中使用 AngularJS 加密信用卡详细信息