[英]Sharing common functionality/configuration across multiple angularjs projects
我有多个angularjs项目,每个项目包含多个页面。 我需要为$ http共享一些通用配置,其中包含一些通用配置设置以及这些项目中的响应拦截器。 我如何在多个angularjs项目中共享这一共同点。 每个项目都加载angularjs,并通过获取bower_dependencies进行构建。
我将不知道每个项目中存在该模块的绝对路径。
具体来说,我需要将withCredentials设置为true,并在请求中设置一些通用标头,并为所有项目设置通用响应拦截器。 有办法分享吗?
我可以以此为基础构建一个通用的angularjs模块吗,如果可以,那么在适当的bower_dependencies下如何工作?
您正在描述的是为什么模块如此方便
每个模块都可以具有自己的配置或运行阶段,服务等,因此将您需要的内容包装到一个模块中并将该模块注入到多个项目中与已使用的其他第三方模块没有什么不同。
angular.module('myUniversalModule', [])
.constant('someconstant',123)
.factory('myUniversalInterceptor', function() {
var myInterceptor = {
....
};
return myInterceptor;
}).config(['$httpProvider', function($httpProvider) {
$httpProvider.interceptors.push('myUniversalInterceptor');
$httpProvider.defaults.withCredentials = true;
}
]);
然后注入任何应用程序
angular.module('thisMainApp', ['myUniversalModule'])
.config(....
.run(....
.factory(....
.constant(...
.controller(...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.