gpt4 book ai didi

javascript - 如何将参数从 .run 传递到 .config?

转载 作者:行者123 更新时间:2023-11-30 11:35:07 24 4
gpt4 key购买 nike

我从事我的 angularjs ui-route 项目。在 .run() 核心中,我将一些变量命名为 clientid$rootScope

有时我需要在 .config() 核心中使用存储的变量。

我知道我不能在 .config() 核心中使用 $rootScope 服务。

所以我的问题是如何将 clientid 变量从 $rootScope 传递到 .config()。也许我可以使用任何其他服务?

最佳答案

当然,使用 $rootScope 是错误的决定。因为没有人喜欢污染全局范围,还有其他原因。您可以考虑创建一个名为 settings 的常量,并在任何 Angular 组件(如 .run block 、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 block 中使用该常量

angular.module('mainModule').run(['settings', runBlock])

function runBlock(settings){
console.log(settings);
}

Note: The reason behind selecting constant is, this flavor can be available as injectable in any angular components.

关于javascript - 如何将参数从 .run 传递到 .config?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44794421/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com