angularjs从哪里开始服务
我想在function app中的angularjs中启动服务。运行方式如下:angularjs从哪里开始服务,angularjs,cordova,service,Angularjs,Cordova,Service,我想在function app中的angularjs中启动服务。运行方式如下: app.run(function($rootScope, $location, $timeout, MessagesService) { if(!MessagesService.isRunning()){ MessagesService.startService(); } } 问题是app.run被调用了两次,即使messageservice已经启动,它也会运行另一个服务实例。。。服务应该是单例的,
app.run(function($rootScope, $location, $timeout, MessagesService) {
if(!MessagesService.isRunning()){
MessagesService.startService();
}
}
问题是app.run被调用了两次,即使messageservice已经启动,它也会运行另一个服务实例。。。服务应该是单例的,所以我不明白发生了什么。或者这是一个启动服务的坏函数
///编辑
我粘贴整个app.run
app.run(function($rootScope, $location, $timeout, AuthenticationService, FlashService, MessagesService, ContactsService) {
if(!MessagesService.isRunning()){
MessagesService.startService();
}
if(!ContactsService.isRunning()){
ContactsService.startService();
}
$rootScope.newContactReq = 0;
$rootScope.newMsgCount = 0;
var routesThatNotRequireAuth = ['/login','/register'];
$rootScope.$on('$routeChangeStart', function(event, next, current) {
if(!_(routesThatNotRequireAuth).contains($location.path())) {
if(!AuthenticationService.isLoggedIn()){
$location.path('/login');
FlashService.show("Please log in to continue.");
}
}
});
$rootScope.$watch(function() {
return $location.path();
},
function(a){
if(!AuthenticationService.isLoggedIn()) {
$rootScope.isUserAuth = 0;
}
else{
$rootScope.isUserAuth = 1;
}
});
});
即使我做了类似的事情
if($rootScope.service === undefined)
$rootScope.service = 1;
它将再次运行app.run,并且$rootScope.service再次未定义 是的,这很奇怪,没有module.run是您应该引导某些服务的地方。您能在这里粘贴更多的代码吗?我编辑了问题并添加了更多的代码。。。