I need to define an angular directive with a link function. (1) The following code works just as expected - but the part about dependency seems to be unnecessarily complicated. Can this be done any cleaner?
(2) Can I use the this
syntax in a link function instead of scope.any
or write the link function as ts-class?
import {myApp} from '../../../angular-modules';
interface MyDirectiveScope extends ng.IScope {
requiredController:any;
}
function MyDirectiveLink(scope: MyDirectiveScope, element: ng.IAugmentedJQuery, attrs: ng.IAttributes, controllers: any) {const vm = scope;
scope.requiredController = controllers[0];
}
export class MyDirective implements ng.IDirective {
constructor(private myService) {
}
public restrict = 'E';
public require = ['^requiredDirective'];
public templateUrl: '/components/myDirective/myDirective.template.html';
public replace = true;
public link = MyDirectiveLink;
static factory(): ng.IDirectiveFactory {
const directive = (myService) => new MyDirective(myService);
directive.$inject = ['myService'];
return directive;
}
}
myApp.directive('myDirective', MyDirective.factory());