I'm working on an application that uses angular as a client side framework, angular currently rocks and I'm really happy using it, though now I find that I use to much copy and paste code that I would like to organize into class hierarchy. For example dialogs share a common set of functionality, they need to be opened, closed, the code that provides typeahead functionality is also a first candidate to inherit from some parent BaseTypeaheadClass, though one thing …
我正在制作一个记录器服务,通过将错误(或调试,如果启用)保存到indexedDB数据库中来扩展angular的$ log服务.这是代码:
angular.module('appLogger', ['appDatabase'])
.service('LogServices', function($log, Database) {
// ...
this.log = function(type, message, details) {
var log = {};
log.type = type
log.context = this.context;
log.message = message;
log.dateTime = moment().format('YYYY-MM-DD HH:mm:ss');
log.details = details || '';
$log[type.toLowerCase()](log);
if (type === 'ERROR' || this.logDebug) {
Database.logSave(log);
}
};
// ...
})
Run Code Online (Sandbox Code Playgroud)
这是按照预期在我的服务中工作.现在的问题是我无法在数据库服务中使用我的记录器,因为它会抛出循环依赖性错误.我理解这个问题,但我不知道如何解决它...我应该如何解决这个问题?
谢谢你的帮助:-)
前段时间,我开始重构我的主项目代码,将业务逻辑从控制器解耦到服务,根据指南.一切顺利,直到我遇到循环依赖(CD)的问题.我读了一些关于这个问题的资源:
不幸的是,对我来说现在还不清楚,如何解决我项目的CD问题.因此,我准备了一个小型演示,它代表了我项目的核心功能:
组件简述:
问题1:gridMainService和gridDataService 之间存在循环依赖关系.第一个gridMainService使用gridDataService来加载具有以下功能的行数据:
self.loadRowData = function () {
// implementation here
}
Run Code Online (Sandbox Code Playgroud)
但是,gridDataService从服务器接收更新,并且必须在网格中插入一些行.所以,它必须使用gridMainService:
$interval(function () {
var rowToAdd = {
make: "VW " + index,
model: "Golf " + index,
price: 10000 * …Run Code Online (Sandbox Code Playgroud) javascript dependency-injection circular-dependency angularjs
我有一些模块定义和服务也与该模块如下
var services=angular.module('app.services', []);
services.factory('ApiService', function($http,$cookies,UserService){
var dataFactory = {};
dataFactory.request=function(url,data,next){
return "Hi";
};
return dataFactory;
});
Run Code Online (Sandbox Code Playgroud)
现在在另一个脚本中,我可以访问模块
services=angular.module('app.services')
Run Code Online (Sandbox Code Playgroud)
但是如何才能从该模块中获取服务实例
apiService=angular.module('app.services').service('ApiService')
Run Code Online (Sandbox Code Playgroud) 我收到了上述错误.以下是一些简化的代码片段.应用:
app = angular.module('app', ['app.classes', 'ngDialog' .....]);
Run Code Online (Sandbox Code Playgroud)
模块配置:
app.config(
function ($httpProvider, $translateProvider, $translatePartialLoaderProvider) {
$translateProvider.preferredLanguage(lang);
$translateProvider.useLoader('$translatePartialLoader', {
urlTemplate: 'api/PartialTranslationLoad?lang={lang}&table={part}'
});
$translatePartialLoaderProvider.addPart('...');
$translatePartialLoaderProvider.addPart('...');
$translateProvider.useSanitizeValueStrategy('sanitize');
$httpProvider.interceptors.push('APIInterceptor');
}
);
Run Code Online (Sandbox Code Playgroud)
拦截器服务在app.classes模块中:
classes = angular.module("app.classes", []);
classes.service('APIInterceptor', function ($q, $rootScope, $location, $window, $injector, ngDialog) {
......
}
Run Code Online (Sandbox Code Playgroud)
错误:
找到循环依赖:$ http < - $ templateRequest < - $ compile < - ngDialog < - APIInterceptor < - $ http < - $ translatePartialLoader
如果我不将ngDialog注入我的拦截器,一切都很好.有人可以解释为什么我得到循环依赖性错误?
谢谢