我在我的天使用户界面路线项目上工作。在.run()核心中,我在名为clientid的变量中添加了一些变量到$rootScope。
在某种程度上,我需要在.config()核心中使用存储的变量。
我知道我不能在$rootScope核心中使用.config()服务。
因此,我的问题是如何将clientid变量从$rootScope传递给.config().Maybe,我可以使用任何其他服务吗?
发布于 2017-06-28 05:53:13
当然,使用$rootScope是错误的决定。因为没有人喜欢污染全球范围,也有其他原因。您可以考虑创建一个名为settings的常量,并在任何角度组件(如.run块、config相位、service、provider等)中使用它。
angular.module('mainModule').constant('settings', {
clientId: 1123 //some inital value
})尽管您可以从配置阶段重写此值。
angular.module('mainModule').config(['settings', configFn])
function configFn(settings){
//routes registers here
//other awesome stuf
//set clientId here
//Assuming your clientId will be available, no async operation.
if(someValue === 1)
settings.clientId = 'asd12';
else
settings.clientId = 'asd34';
}此后,您可以在run块中使用该常量。
angular.module('mainModule').run(['settings', runBlock])
function runBlock(settings){
console.log(settings);
}备注:选择
constant的原因是,这种味道可以在任何角度组件中注射使用。
https://stackoverflow.com/questions/44794421
复制相似问题