如何在第三方库中调用具有依赖项的函数

时间:2020-11-27 18:48:23

I'm new to Angular. My problem is that I can't figure out how to call a function on a provider (that I've created) from within a block of code that is executed under the context of a 3rd party component. Essentially, I don't know how to inject a reference to my provider inside their code.

我是Angular的新手。我的问题是,我无法弄清楚如何在第三方组件的上下文中执行的代码块中调用提供程序(我已创建)上的函数。从本质上讲,我不知道如何在代码中为我的提供者注入引用。

Details: I'm using a 3rd party library (datajs) to make HTTP calls using oData syntax. The library allows me to customize the HTTP request before it is executed. I'm want to modify the request to add an authorization header with a function I've written (setAuthHeaderOnHTTPRequest) hanging off my own 'security' provider.

详细信息:我正在使用第三方库(datajs)使用oData语法进行HTTP调用。该库允许我在执行之前自定义HTTP请求。我想要修改添加授权标头的请求,该标头包含我自己的“安全”提供程序挂起的函数(setAuthHeaderOnHTTPRequest)。

Note: I don't want to modify the 3rd party library.

注意:我不想修改第三方库。

Here is my 'security' provider with the function I want to run (setAuthHeaderOnHTTPRequest)

这是我的“安全”提供程序,它具有我想要运行的功能(setAuthHeaderOnHTTPRequest)

(function() {
angular.module('app.core')
    .factory('security', security);

function security() {
    var service = {
        //...
        setAuthHeaderOnHTTPRequest: setAuthHeaderOnHTTPRequest
    };
    return service;

    function setAuthHeaderOnHTTPRequest(request) {
        // code to add auth header
        return (modifiedRequest);
    }
}
})

Here is the block of code that runs within the context of the 3rd Party component. How do I inject a reference to my security provider within this block of code?

以下是在第三方组件的上下文中运行的代码块。如何在此代码块中为我的安全提供程序注入引用?

function configureODataHTTP() {
        //Create a custom HTTP client for Odata calls see http://*.com/questions/27365519/consuming-web-api-2-odata-endpoint-requiring-windows-authentication-with-breezej
        var defaultHttpClient = OData.defaultHttpClient;
        var myClient = {
            request: function (request, success, error) {
        // this is where I want to call my function on my 'security' provider
                newRequest = security.setAuthHeaderOnHTTPRequest(oldRequest);
                return defaultHttpClient.request(newRequest, success, error);
            }
        };
        OData.defaultHttpClient = myClient;
}

2 个解决方案

#1


You're doing it wrong. Use http interceptors already provided by angularjs framework. https://docs.angularjs.org/api/ng/service/$http#interceptors. Don't use that library as you'd need to wrap it in angularjs, in which you're reproducing efforts.

你这样做是错的。使用angularjs框架已经提供的http拦截器。 https://docs.angularjs.org/api/ng/service/$http#interceptors。不要使用那个库,因为你需要将它包装在angularjs中,你正在重新努力。

#2


I found a solution. I just added a reference to the service I needed to access to $rootScope (after the service started). Then I could access it inside my nested function like this:

我找到了解决方案。我刚刚添加了对访问$ rootScope所需服务的引用(在服务启动后)。然后我可以在我的嵌套函数中访问它,如下所示:

    // this is where I want to call my function on my 'security' provider
            newRequest = $rootScope.security.setAuthHeaderOnHTTPRequest(oldRequest);

I'd really prefer a solution that is less of a hack, but this works.

我真的更喜欢一种不那么黑客的解决方案,但这很有效。

#1


You're doing it wrong. Use http interceptors already provided by angularjs framework. https://docs.angularjs.org/api/ng/service/$http#interceptors. Don't use that library as you'd need to wrap it in angularjs, in which you're reproducing efforts.

你这样做是错的。使用angularjs框架已经提供的http拦截器。 https://docs.angularjs.org/api/ng/service/$http#interceptors。不要使用那个库,因为你需要将它包装在angularjs中,你正在重新努力。

#2


I found a solution. I just added a reference to the service I needed to access to $rootScope (after the service started). Then I could access it inside my nested function like this:

我找到了解决方案。我刚刚添加了对访问$ rootScope所需服务的引用(在服务启动后)。然后我可以在我的嵌套函数中访问它,如下所示:

    // this is where I want to call my function on my 'security' provider
            newRequest = $rootScope.security.setAuthHeaderOnHTTPRequest(oldRequest);

I'd really prefer a solution that is less of a hack, but this works.

我真的更喜欢一种不那么黑客的解决方案,但这很有效。