如何在 Typescript 的 Angular 指令 Class 中添加控制器
How to Add Controller In Angular Directive Class In Typescript
以下是我目前用打字稿编写的 angular 指令 class:
我的问题是如何为该指令添加控制器。我不想创建新控制器 class 并将该控制器与控制器绑定。我想 编写控制器并在 typescript
中的指令 class 中注入 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
将 return 控制器功能,例如:
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
技巧来更方便地声明指令。
以下是我目前用打字稿编写的 angular 指令 class:
我的问题是如何为该指令添加控制器。我不想创建新控制器 class 并将该控制器与控制器绑定。我想 编写控制器并在 typescript
中的指令 class 中注入 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
将 return 控制器功能,例如:
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
技巧来更方便地声明指令。