以下是我到目前为止在 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/