无法在angularjs中注入模块工厂

时间:2016-12-10 13:32:23

标签: javascript angularjs

我收到了angular.module('MyApp', ['ngRoute','authService']){ });

的错误

我有app.js

angular.module('MyApp')
  .factory('authService', function($http) {

  });

和文件调用auth.js,它有

  <script src="/js/app.js"></script>
  <script src="/js/services/auth.js"></script>

我确实将它们包含在index.html

class BlockLoginListener {

    private $attempt;
    private $router;


    public function __construct(SessionInterface $session, RouterInterface $router){
        $this->attempt = $session->get(LoginAttempt::LOGIN_ATTEMPT, null);
        $this->router = $router;
    }

    public function onKernelRequest(GetResponseEvent $event){

        if ($event->getRequestType() !== \Symfony\Component\HttpKernel\HttpKernel::MASTER_REQUEST) {
            return;
        }

        if(null !== $this->attempt){
            if($this->attempt->isLocked()){
                $message = sprintf('Too much attempts, your account has been locked for %d minutes', $this->attempt->getLockInterva());

                $url = $this->router->generate("test",array("message" => $message));

                //$event->stopPropagation();
                $event->setResponse(new RedirectResponse($url));

            }
        }

    }
}

出了什么问题?

1 个答案:

答案 0 :(得分:0)

您无法注入factory to a module

angular.module('MyApp', ['ngRoute']){

});

你的控制器可以按如下方式注入工厂,

app.controller('AppCtrl', function($scope,authService) {
}

您已经使用此代码添加到模块中,因此无需再次注入

angular.module('MyApp')
  .factory('authService', function($http) {

  });

如果您想让工厂在不同的模块中,请在另一个模块中使用它,如下所示。

angular.module('MyApp2')
  .factory('authService', function($http) {

  });

然后您可以在第一个应用程序中使用,如下所示,

angular.module('MyApp', ['ngRoute','MyApp2']){

});