[英]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? 我想在angularjs的配置阶段访问存储在Cookie中的值,该怎么办? Is it possible?
可能吗? I currently have the below code inside app.config(function(..., $translateProvider){...}) which is working perfectly fine:
我目前在app.config(function(...,$ translateProvider){...})内部有以下代码,它工作得很好:
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: 我想在app.config()中实现类似(以下代码)的功能,但我认为在配置阶段仅注册和访问了提供程序,因此我应该如何通过$ cookies服务或任何其他方式访问配置阶段存储在cookie中的值方式:
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. 更一般而言,应在.run()阶段检索$ cookie。 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().
我建议您在.config()中加载所有$ translate局部语言和语言,并在.run()中使用cookie设置活动语言。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.