Pregunta

Why does i can't inject services like this?

service('actionService',function (sessionService) {
 this.doAction = function () {
  return 5;
 }
});
service('sessionService',function (userService) {
 this.get = function {
    if(userService.check()){
    //do other stuffs 
    }
  }

});
service('userService',function (actionService) {
  this.check = function () {
  actionService.doAction();
  if(x = 9){ 
    return true;
  }else{ 
   return false; 
  } 
  }
});

I get "Circular injection error" in console

how to handle this now ?

here a better code : http://pastebin.com/wS32UNCq

¿Fue útil?

Solución

Before you can have an instance of actionService, you need to inject an instance of sessionService.

Before you can have an instance of sessionService, you need to inject an instance of userService.

Before you can have an instance of userService, you need to inject an instance of actionService.

It's a chicken or the egg problem... because Angular uses constructor injection, it can't instantiate a service until all of its dependencies are satisfied. But the dependencies go around in a circle, none of them can ever be instantiated.

To solve this problem, you will need to extract a shared dependency out to an additional service which can be instantiated without dependencies.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top