2

以下是我迄今为止编写的打字稿中的角度指令类:

我的问题是如何为这个指令添加控制器。我不想创建新的控制器类并将该控制器与控制器绑定。我想编写控制器并将 ISOLATE SCOPE 注入到 typescript 的指令类

    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());
}
4

1 回答 1

1

您实际上可以声明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技巧来更方便地声明指令。

于 2015-12-13T00:57:13.210 回答