对于我网站上的所有angular应用程序,我都有相同的配置块,但都位于不同的文件中。
app_1.config([
"$httpProvider", function($httpProvider) {
$httpProvider.defaults.headers.common['X-CSRF-Token'] = $('meta[name=csrf-token]').attr('content');
}
]);
app_2.config([
"$httpProvider", function($httpProvider) {
$httpProvider.defaults.headers.common['X-CSRF-Token'] = $('meta[name=csrf-token]').attr('content');
}
]);
app_3.config([
"$httpProvider", function($httpProvider) {
$httpProvider.defaults.headers.common['X-CSRF-Token'] = $('meta[name=csrf-token]').attr('content');
}
]);
有没有抽象的标准方法?
您可以创建另一个模块,例如“ myApp.common”或“ myApp.common.configs”,并将您的通用实现保留在该模块中,并将该模块作为依赖项包含在需要它们的其他模块中。
例子:-
/*Create an app that has the common configuration used in your app clusters*/
angular.module('app.common', []).config([
"$httpProvider", function($httpProvider) {
$httpProvider.defaults.headers.common['X-CSRF-Token'] = $('meta[name=csrf-token]').attr('content');
}
]);
和
//Include common module as well as a part of other dependencies your app may have
var app_1 = angular.module('app1', ['app.common', 'somedep', ...]);
var app_2 =angular.module('app2', ['app.common']);
//...
附带一提,我将避免像示例中那样将模块存储在全局变量中,而是在需要时更喜欢使用模块getter语法。例如: - angular.module('app1').service(...
,angular.module('app1').config(...
等等。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句