如何在Angular中调用第三方函数

huangapple go评论76阅读模式
英文:

How to call the functions of third party in Angular

问题

这是您要翻译的内容:

I have a payment gateway integrated into my application. I am working on saving credit card and ach details. So I am calling an IFrame to load the credit card and ach forms. In this form there is an option to select the mode of payment. So while switching between the radio buttons I need the values of selected payment method in my console, there is a prebuilt function to do so in the doc. But if I define that function in the component then it is not getting called.

This is the core JS code I am referring to:

<script type="text/javascript">
    ClearentSDK.init({
        "baseUrl": "https://gateway-sb.clearent.net",
        "pk": "YOUR PUBLIC KEY GOES HERE",
        "enableAch":true
    });
 function ClearentOnPaymentTypeChange(paymentType) {
        console.log("Payment type was changed to ", paymentType) // this is the function to log which payment type is selected.
}

My Angular code:

setClearentToken() {
    this.isLoadingResults = false;
    ClearentSDK.init({
      "baseUrl": "https://gateway-sb.clearent.net",
      "pk": "Your Public Key", //I am providing the actual public key here. Just removed due the security.
      "enableAch": true
    });
  }

  ClearentOnPaymentTypeChange(paymentType) {
    console.log("Payment type was changed to ", paymentType);
  }
英文:

I have a payment gateway integrated into my application. I am working on saving credit card and ach details. So I am calling an IFrame to load the credit card and ach forms. In this form there is an option to select the mode of payment. So while switching between the radio buttons I need the values of selected payment method in my console, there is a prebuilt function to do so in the doc. But if I define that function in the component then it is not getting called.

This is the core JS code I am referring to:

&lt;script type=&quot;text/javascript&quot;&gt;

    ClearentSDK.init({
        &quot;baseUrl&quot;: &quot;https://gateway-sb.clearent.net&quot;,
        &quot;pk&quot;: &quot;YOUR PUBLIC KEY GOES HERE&quot;,
        &quot;enableAch&quot;:true
    });
 function ClearentOnPaymentTypeChange(paymentType) {
        console.log(&quot;Payment type was changed to &quot;, paymentType) // this is the function to log which payment type is selected.
}

My Angular code:

setClearentToken() {
    this.isLoadingResults = false;
    ClearentSDK.init({
      &quot;baseUrl&quot;: &quot;https://gateway-sb.clearent.net&quot;,
      &quot;pk&quot;: &quot;Your Public Key&quot;, //I am providing the actual public key here. Just removed due the security.
      &quot;enableAch&quot;: true
    });
  }

  ClearentOnPaymentTypeChange(paymentType) {
    console.log(&quot;Payment type was changed to &quot;, paymentType);
  }

答案1

得分: 1

窗口标签的HTML中使用时,可以这样使用:

window.ClearentSDK.init({
  // ...
  // ...
});

此外,您需要将SDK添加到TypeScript中窗口的接口以便识别它:

declare global {
  interface Window {
    ClearentSDK?: any;
  }
}
英文:

since you are using it in the script tag in html, you can use it like this:

window.ClearentSDK.init({
...
...

also, you'll have to add the sdk in the window's interface for typescript to identify it.

   declare global {
      interface Window {
        ClearentSDK?: any;
      }
   }

huangapple
  • 本文由 发表于 2023年2月24日 14:00:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/75553088.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定