Angularjs 与动态控制器共享指令隔离作用域

Angularjs 与动态控制器共享指令隔离作用域,angularjs,typescript,Angularjs,Typescript,我正在编写一个TypeScript AngularJS应用程序,遇到了麻烦。我需要将控制器动态应用于指令,因为该指令提供了一个动态UI来适应不同的任务。我需要能够在控制器中动态替换以处理指令中的不同活动 我在网站上找到了回答如何做到这一点的信息,提供了以下信息: export interface IHostScope extends ng.IScope { type: string; title: string; subtitle: string; } export class H

我正在编写一个TypeScript AngularJS应用程序,遇到了麻烦。我需要将控制器动态应用于指令,因为该指令提供了一个动态UI来适应不同的任务。我需要能够在控制器中动态替换以处理指令中的不同活动

我在网站上找到了回答如何做到这一点的信息,提供了以下信息:

export interface IHostScope extends ng.IScope {
  type: string;
  title: string;
  subtitle: string;
}

export class Host implements ng.IDirective {
  public templateUrl: "/some/template/url";
  public restrict: "E";
  public replace: true;
  public controller = "@";
  public name = "controllerName";
  public scope: Object {
    type: "@",
    title: "@",
    subtitle: "@"
  };
  public link: Function = (scope: IHostScope, element: ng.IAugmentedJQuery, attrs: ng.IAttributes): void => {
    //Stuff happens in here
  }
}

export class ControllerA {
  constructor(private: $scope: IHostScope, private $state: ng.ui.IStateService) {
    //$scope and $state are null at runtime
  }
}


module.directive("host", <any>Host);
module.controller("ControllerA", ControllerA);
导出接口IHostScope扩展了ng.isScope{
类型:字符串;
标题:字符串;
字幕:字符串;
}
导出类主机实现ng.IDirective{
公共模板url:“/some/template/url”;
公共限制:“E”;
公共替换:正确;
公共控制器=“@”;
public name=“controllerName”;
公共范围:对象{
键入:“@”,
标题:“@”,
副标题:“@”
};
公共链接:Function=(作用域:IHostScope,元素:ng.IAugmentedJQuery,属性:ng.IAttributes):void=>{
//这里发生了很多事情
}
}
出口级控制器{
构造函数(private:$scope:IHostScope,private$state:ng.ui.IStateService){
//$scope和$state在运行时为空
}
}
模块指令(“主机”,主机);
模块控制器(“ControllerA”,ControllerA);

问题是,在运行时,$state和$scope值为null,我无法访问这些变量上的现有属性,这些变量可能是由指令上的link函数设置的。如何在指令和动态控制器之间共享作用域,并使$state实际填充?

当然,当我发布这篇文章时,我意识到答案非常简单。我需要将服务注入控制器,然后它会自行解决

在构造函数之前的ControllerA顶部添加$inject语句,如下所示:

export class ControllerA {
  static $inject = ["$scope", "$state"];
  constructor(private $scope: IHostScope, private $state: ng.ui.IStateService) {
    //Do stuff - works like a charm
  }
}

当然,当我发布这篇文章时,我意识到答案很简单。我需要将服务注入控制器,然后它会自行解决

在构造函数之前的ControllerA顶部添加$inject语句,如下所示:

export class ControllerA {
  static $inject = ["$scope", "$state"];
  constructor(private $scope: IHostScope, private $state: ng.ui.IStateService) {
    //Do stuff - works like a charm
  }
}