0

Problem is I can't insert constant from one module to another's module config.

myApp.module.js:

'use strict';

angular.module('myApp', [
    'myApp.base',
    'ui.router',
    'ui.bootstrap',
    'myApp.common'
]);

myApp.config.js

'use strict';

angular
    .module('myApp')
    .constant('appConfig', applicationConfiguration())

function applicationConfiguration() {
    var config = {
        debug: true
    };

    switch (window.location.hostname) {
        case 'www.myapp.net':
            config.apiUrl = 'www.myapp.net/';
            config.publicSiteUrl = "www.myapp-public.net";
            break;
        case 'localhost':
            config.apiUrl = 'http://localhost:61057/';
            config.publicSiteUrl = "http://localhost:57563/";
            break;
    }

    return config;
}

This is where I am trying to use it:

'use strict';

angular.module('myApp.base', [
    'myApp'
])
    .config(['$stateProvider','appConfig', function configState($stateProvider, appConfig) {

        $stateProvider
            .state('public', {
                ...
            })
            .state('publicSite', {
                url: appConfig.publicSiteUrl,
                external: true
            });
    }])
    .run(function(){});

When I run the code, I keep on getting following error:

Error: [$injector:modulerr] Failed to instantiate module myApp due to:
[$injector:modulerr] Failed to instantiate module myApp.base due to:
[$injector:unpr] Unknown provider: appConfig
7
  • Change .config(['appConfig', function configState($stateProvider, appConfig) { to .config(['$stateProvider', 'appConfig', function configState($stateProvider, appConfig) { Commented Feb 1, 2017 at 6:01
  • @Tushar it didn't work with that too. I already tried that. Commented Feb 1, 2017 at 6:02
  • Change .constant('appConfig', applicationConfiguration()) to .constant('appConfig', applicationConfiguration) Removed invocation of function. Commented Feb 1, 2017 at 6:03
  • change .module('myApp') to .module('myApp',[]) Commented Feb 1, 2017 at 6:04
  • @Tushar Didn't work Commented Feb 1, 2017 at 6:09

1 Answer 1

1

I don't know exactly what are you doing! :| (put constants in App, instead of base, and put routing in base instead of App, or reason of don't putting them together and so much other questions)
But maybe this will solve your problem.

myApp.module.js:

'use strict';

angular.module('myApp', [
    'ui.bootstrap',
    'myApp.common'
]);

myApp.config.js

'use strict';

angular
    .module('myApp')
    .constant('appConfig', applicationConfiguration)

function applicationConfiguration() {
    var config = {
        debug: true
    };

    switch (window.location.hostname) {
        case 'www.myapp.net':
            config.apiUrl = 'www.myapp.net/';
            config.publicSiteUrl = "www.myapp-public.net";
            break;
        case 'localhost':
            config.apiUrl = 'http://localhost:61057/';
            config.publicSiteUrl = "http://localhost:57563/";
            break;
    }

    return config;
}

use it:

'use strict';

angular.module('myApp.base', [
    'myApp',
    'ui.router',
])
    .config(['$stateProvider','appConfig', function configState($stateProvider, appConfig) {

        $stateProvider
            .state('public', {
                ...
            })
            .state('publicSite', {
                url: appConfig.publicSiteUrl,
                external: true
            });
    }])
    .run(function(){});
Sign up to request clarification or add additional context in comments.

1 Comment

Myy references and injections were not right. There was a circular reference in code. This was just a simple representation of code. The real code was way more complex, but your sample did help me in revisit my code.

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.