我有几个 AngularJS 应用程序,它们都有一些共同的依赖项,例如 angular-translate
。所有 Angular 应用程序都具有相同的 Angular-translate 配置(在 app.js
中)。
我正在寻找一种方法来外部化所有这些应用程序中的 angular-translate
配置。在某种程度上,我将在一个地方(也许是服务?)进行更改,然后配置将应用于应用程序。
顺便说一句,我是 Angular 世界的新手,您的准确建议将不胜感激。
最佳答案
您可以在单独的项目中创建 Angular 模块配置函数,项目中的所有项目都会导入并使用该函数。 Angular 允许您拥有任意数量的 module().config 函数。 .run 函数也是如此。
示例
// Some common file available to all projects
angular.module('common-config', [
'angular-translate'
/* other common dependencies here */
]).config(['$translateProvider',
function ($translateProvider) {
$translateProvider.preferredLanguage('en');
// Other configuration here.
}]);
// App 1
angular.module('app-one', ['common-config']).run(/*...*/);
// App 2
angular.module('app-two', ['common-config']).run(/*...*/);
// App 3
angular.module('app-three', ['common-config']).run(/*...*/);
关于javascript - 在具有相同配置的多个 Angular 应用程序中外部化服务配置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30755461/