Wydaje się, że istnieje wiele sposobów tworzenia dyrektyw Angular w języku TypeScript. Najchętniej widziałem, jak używać statycznej funkcji fabrycznej:Zastrzyki zależne od dyrektywy kątowej - TypeScript
module app {
export class myDirective implements ng.IDirective {
restrict: string = "E";
replace: boolean = true;
templateUrl: string = "my-directive.html";
link: ng.IDirectiveLinkFn = (scope: ng.IScope, el: ng.IAugmentedJQuery, attrs: ng.IAttributes) => {
};
static factory(): ng.IDirectiveFactory {
var directive: ng.IDirectiveFactory =() => new myDirective();
return directive;
}
}
angular.module("app")
.directive("myDirective", myDirective.factory());
}
Ale nie jestem pewien, co zrobić, jeśli muszę coś wstrzyknąć. Chciałbym powiedzieć $ timeout:
module app {
export class myDirective implements ng.IDirective {
restrict: string = "E";
replace: boolean = true;
templateUrl: string = "my-directive.html";
constructor(private $timeout: ng.ITimeoutService) {
}
link: ng.IDirectiveLinkFn = (scope: ng.IScope, el: ng.IAugmentedJQuery, attrs: ng.IAttributes) => {
// using $timeout
this.$timeout(function(): void {
}, 2000);
}
static factory(): ng.IDirectiveFactory {
var directive: ng.IDirectiveFactory =() => new myDirective(); // Uhoh! - What's goes here?
directive.$inject = ["$timeout"];
return directive;
}
}
angular.module("app")
.directive("myDirective", myDirective.factory());
}
Jak widać powyżej, nie jestem pewien, jak nazywają myDirective contructor i przechodzą w $ timeout.
Czemu nakładania własnych modułów na szczycie kątowe? Jakie korzyści daje dodatkowa warstwa złożoności, dodatkowy stan globalny i duplikacja semantyczna ?. – yangmillstheory