简体   繁体   中英

Accessing constant or cookie value in configuration phase of angularjs

I want to access values stored in Cookie in config phase of angularjs, how can I do it? Is it possible? I currently have the below code inside app.config(function(..., $translateProvider){...}) which is working perfectly fine:

var defaultLang = 'de-DE';
var moduleName = { moduleName: 'UICaption.Resources' };

    $translateProvider.useLoader('customLocalizationLoaderFactory',
    {        
        moduleName, url: resourceUrl
    });

    $translateProvider.preferredLanguage(defaultLang);

I want to achieve something like(below code) inside app.config(), but I think only providers are registered and accessed during configuration phase, so how should I access the values stored in cookies in config phase via $cookies service or any other way:

 var defaultLang = $cookies.get('ASPNET_CULTURE' === 'de-DE') ? 'de-DE' : 'en-US';
    var moduleName = { moduleName: 'UICaption.Resources' };

    $translateProvider.useLoader('customLocalizationLoaderFactory',
    {
        moduleName, url: resourceUrl
    });

    $translateProvider.preferredLanguage(defaultLang);

Please guide if any other approach is there for the same?

Thank you.

This old answer might help you.

More generally, $cookies are meant to be retrieved in the .run() phase. I suggest you to load all your $translate partials and languages in the .config() and the set an active language using your cookie in your .run().

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM