AngularJS 依赖注入在模块中失败

AngularJS Dependency Injection Fails in module

本文关键字:失败 模块 依赖 注入 AngularJS      更新时间:2023-09-26

我对角度依赖注入机制有问题。我在一个独立的模块中编写了一个指令,它在数据表上做了一些简洁的事情。我正在使用TypeScript,我的指令如下所示:

export class MdListControls implements angular.IDirective {
  public templateUrl = "/app/templates/mdListControls.html";
  public static instance(): any {
    return new MdListControls();
  };
  public restrict = 'E';
  public transclude = true;
  public replace = true;
  public scope = {
    grid: '=',
  };
  public controller = "mdListControlsController"
  public controllerAs = "vm";
  public link = function (scope: angular.IScope, element: angular.IAugmentedJQuery, attrs: angular.IAttributes) {
  };
}

我的控制器如下所示:

export class MdListControlsController { 
  static $inject = ['scope']
  constructor($scope: angular.IScope){
    //Setup the constructor
  }
  //Here goes the controller Logic
}

我正在新模块上注册指令和控制器,如下所示:

angular.module("base.directives.mdListControls", [])
  .controller("mdListControlsController", MdListControlsController)
  .directive("mdListControls", function () { return MdListControls.instance() });
但是,一旦

我运行该应用程序,一旦加载指令,就会出现以下错误:

Argument 'mdListControlsController;' is not a function, got undefined

起初,这看起来不太像是依赖注入的问题。但是当我在应用程序本身上注册控制器时(它被注入了"base.directives.mdListControls"模块),就像这样

app.controller("mdListControlsController", MdListControlsController);  

一切正常,控制器被正确注入指令中。

有谁知道我在这里做错了什么?感谢您的帮助!

在 MdListControls 类中,尝试提供控制器类,而不是控制器名称。

...    
public scope = {
    grid: '=',
  };
public controller = MdListControlsController
public controllerAs = "vm";
...