0

I'm building an angular module that it is a library of functions for call some Rest Services in the BackEnd, the problem is that this module it is centralized and the Rest Api are in various domain, one for each user. I would like to run my module in the client initialized with the string of his domain, so they will call the right Rest Service. This one is a stupid example of the root angular js in the client app, I know it is wrong but its rapresents well the goal i would like to achieve.

angular.module('UserApp', ['MyRestLibrary("user_domain")'];

Hope to be clear, and sorry for the bad english. Thanks for all the suggestions

1 Answer 1

1

Look into the Providers: https://docs.angularjs.org/guide/providers

You can try converting your Services/Factories into Providers and, when the module is initialized, configure it with the domain. Something like this:

// Create your service as a provider
angular
    .module('MyRestLibrary')
    .provider('myRestService', function() {

        this.domain = '';

        this.$get = function($http) {
            var domain = this.domain;
            return {
                myRestMethod: function() {
                    return $http.get(domain + '/myRestMethodUrl');
                }
            }
        };

        this.setDomain = function(domain) {
            this.domain = domain;
        };
    });

// Configure your module   
angular
    .module('MyRestLibrary')
    .config(function(myRestServiceProvider) {
        myRestLibraryProvider.setDomain('user_domain');
    });

// Use your module
angular
    .module('UserApp', ['MyRestLibrary']);

Here's a working JSFiddle example: http://jsfiddle.net/dvj0n539/

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.