I am currently working on an older project that was built with AngularJS 1.5.7 and I want to incorporate TypeScript for new additions. This means only the newly added features will be in TypeScript, without a complete migration at this point.
Through my research, I have figured out how to create a directive using TypeScript in AngularJS.
import * as angular from 'angular';
angular
.module('starter.directives')
.directive('lateralScrollbar', ['$timeout', ($timeout) =>
new (class {
restrict = 'A';
constructor(
private $timeout
) { }
link(scope: ng.IScope, element: ng.IAugmentedJQuery, attrs: ng.IAttributes) {
let directive = this;
new (class {
constructor(
scope: ng.IScope,
element: ng.IAugmentedJQuery,
attrs: ng.IAttributes
) {
scope.something = 'Loading';
/** 1. How to optimize that "directive" is not needed here? Maybe that I can use "this" instead or "parent" */
directive.$timeout(() => scope.something = 'OK', 1000);
}
})(scope, element, attrs);
}
/** 2. How to optimize so that I can pass the arguments with which the function was called. For example fn.apply(); and ...args? */
})($timeout)
]);
There are two comments within this code snippet.
- How can I optimize so that "directive" is not required here? Perhaps I could utilize "this" instead of "parent".
- How can I optimize to pass the arguments used when calling the function? For instance, fn.apply(); and ...args?
The first comment raises an interesting point. Currently, I call the directive normally with an array of dependencies and include the function at the end. However, this process seems unnecessarily complex. The function creates an object of the class within it, which then has a link function that defines the directive
as this
, allowing me to access the passed arguments (e.g., $timeout).
Additionally, I notice that I define the $timeout
variable four times. Once in the Angular directive's dependencies, once in the class constructor, once as an argument for initial class initialization, and finally in the function which wraps the first class.
This experiment failed because it couldn't handle the ...args
concept. I'm unsure how to further optimize this.
import * as angular from 'angular';
angular
.module('starter.directives')
.directive('lateralScrollbar', ['$timeout', (...args) =>
new (class {
restrict = 'A';
constructor(
private $timeout
) { }
link(scope: ng.IScope, element: ng.IAugmentedJQuery, attrs: ng.IAttributes) {
let directive = this;
new (class {
constructor(
scope: ng.IScope,
element: ng.IAugmentedJQuery,
attrs: ng.IAttributes
) {
scope.something = 'Loading';
/** 1. How to optimize that "directive" is not needed here? Maybe that I can use "this" instead or "parent" */
directive.$timeout(() => scope.something = 'OK', 1000);
}
})(scope, element, attrs);
}
/** 2. How to optimize that I can pass the arguments with that the function was called. For example fn.apply(); and ...args? */
})(args)
]);
Error: TS2554: Expected 1 arguments, but got 0.
If anyone can offer assistance, I would greatly appreciate it! Thank you in advance!