gpt4 book ai didi

javascript - 在父模块上运行的 block 是否会在子模块中的代码之前运行?

转载 作者:可可西里 更新时间:2023-11-01 16:29:57 26 4
gpt4 key购买 nike

考虑这样一个 Angular 应用程序(写在我的手机上,对于奇怪的语法感到抱歉):

angular.module('app1').controller(ctrl1, function($http){
$http.get(...);
});

angular.module('app2', ['app1']).run(function($http){
$http.default.headers.common.foo = 'bar';
});

运行 block 是否保证在 Controller 代码之前运行?换句话说,如果我通过调用 app2 加载我的页面,是否可以保证所有 HTTP 调用都具有默认 header ?

最佳答案

实际上,“运行” block 将在运行阶段调用,也就是 Controller 初始化之前。

但是,我建议您通过 $httpProvider.interceptors 在配置阶段配置默认 header :

app.factory('httpRequestInterceptor', function () {
return {
request: function (config) {
config.headers['foo'] = 'bar';
return config;
}
};
});

app.config(function ($httpProvider) {
$httpProvider.interceptors.push('httpRequestInterceptor');
});

关于javascript - 在父模块上运行的 block 是否会在子模块中的代码之前运行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38151949/

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