Javascript 将工厂注入控制器angularjs+;打字稿

Javascript 将工厂注入控制器angularjs+;打字稿,javascript,angularjs,typescript,angularjs-factory,Javascript,Angularjs,Typescript,Angularjs Factory,我一直试图使用Angularjs和Typescript将工厂注入控制器,但我得到了以下错误error:[$injector:unpr]http://errors.angularjs.org/1.2.9/$injector/unpr?p0=AuthenticationProvider%20%3C-%20身份验证 我一直在研究,但找不到解决方案,因为我所做的与一些解决方案类似 这是我的登录控制器模块 import Authentication = require('AuthenticationSer

我一直试图使用Angularjs和Typescript将工厂注入控制器,但我得到了以下错误
error:[$injector:unpr]http://errors.angularjs.org/1.2.9/$injector/unpr?p0=AuthenticationProvider%20%3C-%20身份验证

我一直在研究,但找不到解决方案,因为我所做的与一些解决方案类似

这是我的登录控制器模块

import Authentication = require('AuthenticationService');
module LoginController{

export interface UIMainScope extends ng.IScope {
    login: (credential: any) => void;
}

export class Controller {
    static $inject = ['$scope', '$http', 'Main', 'Authentication'];
    constructor (){}
}
export = LoginController
angular.module('App').controller('Controller', LoginController.Controller);

我忘了在这里注入一些东西来调用该方法了吗?

这里的问题与这样一个事实有关,angular的
$inject
不能从Typescript
require
中获益。这些是独立的特征,它们代表不同的概念

angular的
[$inject][1]
作为内置工具,只能注入已注册的内容。它没有任何对Typescript对象的访问权——只访问它自己的(angular的)对象工厂

如错误所示:未知提供程序“AuthenticationProvider”,我们必须调用:

angular.module('App')
  .factory('Authentication', [... its dependencies ...
      , (params) => new Authentication(params)]);
现在,我们已经在angular中准备好了身份验证,我们可以在控制器内部请求它

语法可能如下所示:

// module with Scope and Controller
module LoginController
{
  export interface UIMainScope extends ng.IScope 
  {
    login: (credential: any) => void;
  }

  export class Controller
  {
    // constructor, instantiating private members this.$scope ...
    constructor($scope: UIMainScope
       , $http :ng.IHttpService
       , Main: any
       , Authentication: any)
    ...
  }
}

// and finally pass it into angular as .controller()
angular.module('App')
  .controller('Controller', [
     '$scope', '$http', 'Main', 'Authentication',
     ($scope, $http, Main, Authentication)  =>
     {
        return new LoginController.Controller($scope, $http, Main, Authentication);
     }
]);
最后:如果
Authentication
提供者不能从Angular发起的Being中获益,我们也可以使用它。我们根本不需要注册它。只需调用require,并访问其方法即可。。。但是我们必须跳过angular–s
$inject
过程


这可能不是这种情况,但是想象一下一些
QueryStringBuilder
。。。在方法
Builder.build(…)

中可能只使用传递的参数,但您似乎没有向以下用户注册
身份验证
服务:

angular.module('App').controller('Controller', LoginController.Controller)
    // fix based on your code 
    .service('Authentication',Authentication.AuthenticationService); 

此视频可能会有所帮助:

这与缺少声明
.factory('Authentication')
有关。如果这是在另一个模块(例如“Auth”)中,请不要忘记在
angular.module('App',[…此处引用类似“Auth]”)中引用该模块。
。我回答的最后一行表示标准的角度
.controller()
定义。该错误表示缺少某些依赖项时的标准情况。。。它有用吗?是的。非常感谢。当我这样做时,我想:静态$inject=['$scope','$http','UIMainUIModelService','Authentication'];=>这基本上是注入到模块中,而导入将导入并引用模块?
$inject
是角度世界,
import
是typescript世界。他们完全独立。不同的概念。
$inject
只能注入已注册的内容。它无法访问导入的TS内容。因此,如果您将在angular中注册
.factory('Authentication')
(如果我们喜欢TS,它的方式/样式将与我为控制器显示的方式/样式几乎相同),那么您可以要求$inject使用它。。。希望现在更清楚;)这种解释现在很有道理。当您说注册到Angular时,您的意思是在应用程序本身或控制器模块中注册吗?是通过应用程序模块内部的工厂()将其注入