Lets say I have a simple angular directive that looks like this:
app.directive('setFocus', ['$timeout', function($timeout) {
return {
restrict: 'AC',
link: function(_scope, _element) {
$timeout(function() {
_element[0].focus();
}, 0);
}
};
}]);
How can I write this using Typescript and get the $timeout accesible within the link function? My example would look something like this:
/// <reference path="../../reference.ts"/>
class SetFocus{
constructor() {
var directive: ng.IDirective = {};
directive.restrict = 'EA';
directive.scope = { };
directive.link= function ($scope, $element, $attrs) {
// How can I access $timeout here?
}
return directive;
}
}
directives.directive('setFocus', [SetFocus]);
This might be a silly example but it is the principle I would like to get working, which is using injected dependencies in the angular link function.
Guide to using TypeScript in AngularJS applications. Supporting older AngularJS applications doesn't mean you can't take advantage of modern tools like TypeScript. Not every file in your application needs to be written in TypeScript at once, you just can rename all your JavaScript files to have a . ts extension.
Dependency Injection in AngularJS can be defines as the software design pattern which defines the way the software components are dependent on each other. AngularJS provides a set of components that can be injected in the form of dependencies such as factory, value, constant, service, and provider.
Injecting a value into an AngularJS controller function is done simply by adding a parameter with the same name as the value (the first parameter passed to the value() function when the value is defined). Here is an example: var myModule = angular. module("myModule", []); myModule.
Dependency Injection Dependency Injection (DI) is a software design pattern that deals with how components get hold of their dependencies. The AngularJS injector subsystem is in charge of creating components, resolving their dependencies, and providing them to other components as requested.
Dependencies are singletons within the scope of an injector. In this guide's example, a single HeroService instance is shared among the HeroesComponent and its HeroListComponent children. However, Angular DI is a hierarchical injection system, which means that nested injectors can create their own service instances.
Dependency Injection (DI) is a software design pattern that deals with how components get hold of their dependencies. The AngularJS injector subsystem is in charge of creating components, resolving their dependencies, and providing them to other components as requested.
The AngularJS injector subsystem is in charge of creating components, resolving their dependencies, and providing them to other components as requested. Using Dependency Injection
Try this way:
class SetFocus implements ng.IDirective {
//Directive settings
restrict :string = 'EA';
scope : any= {};
//Take timeout argument in the constructor
constructor(private $timeout: ng.ITimeoutService) {
}
link: ng.IDirectiveLinkFn = ($scope: ng.IScope, $element: ng.IAugmentedJQuery, $attrs: ng.IAttributes) => {
//refer to the timeout
this.$timeout(function() {
$element[0].focus();
}, 0);
}
//Expose a static func so that it can be used to register directive.
static factory(): ng.IDirectiveFactory {
//Create factory function which when invoked with dependencies by
//angular will return newed up instance passing the timeout argument
var directive: ng.IDirectiveFactory =
($timeout:ng.ITimeoutService) => new SetFocus($timeout);
//directive's injection list
directive.$inject = ["$timeout"];
return directive;
}
}
directives.directive('setFocus', SetFocus.factory());
It could be a problem with the way you have it right now. Because directive factory is not newed up so its constructor will execute with this
as global object. This way you don't end up having a huge constructor as well and can write it in proper class ey manner.
If you have many dependencies injected instead of repeating the arguments in the factory you could as well do:
var directive: ng.IDirectiveFactory =
(...args) => new (SetFocus.bind.apply(SetFocus, [null].concat(args)));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With