javascript - 如何在 Typescript 的 Angular 指令类中添加 Controller

标签 javascript angularjs typescript angular-directive angular-controller

以下是我到目前为止在 typescript 中编写的 Angular Directive(指令)类:

我的问题是如何为该指令添加 Controller 。我不想创建新的 Controller 类并将该 Controller 与 Controller 绑定(bind)。我想在 typescript 中编写 Controller 并在指令类中注入(inject) ISOLATE SCOPE

    module Sheet.Directive{
    class InputControl implements ng.IDirective {
    restrict = 'A';
    //require = 'ngModel';
    templateUrl = "../Templates/inputcontrol.html";



    constructor(private $location: ng.ILocationService) {
    }

    link = (scope: ng.IScope, element: ng.IAugmentedJQuery, attrs: ng.IAttributes, ctrl: any) => {
        console.log(this.$location);
    };

    static factory(): ng.IDirectiveFactory {
        const directive = ($location: ng.ILocationService) => new InputControl($location);
        directive.$inject = ['$location'];
        return directive;
    }
}

angular.module("SheetApp").directive("inputControl", InputControl.factory());
}

最佳答案

您实际上可以声明将返回 Controller 函数的属性 controller,例如:

export class App {
  restrict='E';
  templateUrl='src/app.html';
  scope = {
    a : '@'
  }
  controller = ['$scope',($scope) => {
    console.log("this this controller",$scope.a);
  }];
}

这是 Plunker 中的示例:http://plnkr.co/edit/j4coAAZ207RHyGsqFPgC (我使用了我的 @directive 技巧来更方便地声明指令。

关于javascript - 如何在 Typescript 的 Angular 指令类中添加 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34246275/

相关文章:

javascript - 模式无法打开

javascript - React-Native 博览会 POST Blob

javascript - 在导出进行单元测试之前等待申请?

javascript - 为什么 JavaScript string.split(/[ -_]+/) 的行为就像引号 (' and ") 包含在 [group] 中一样?

angularjs - AngularJS 中的模型 View ViewModel

typescript - typescript 中的复杂对象

javascript - 如果未提供,TypeScript 将参数设置为可选

javascript - 无法将复选框值推送到数组

javascript - 如何在 AngularJS 中的某些事件完成后删除绑定(bind)在 html 元素内的动态数据

mysql - Ionic-Angularjs-Restful-webService 项目结构