का उपयोग कर मैं एक साधारण कोणीय निर्देश है कि इस तरह दिखता है का कहना है कि चलो AngularJS निर्देश के लिए निर्भरता सम्मिलित करें? मेरे उदाहरण कुछ इस तरह दिखेगा:टाइपप्रति
/// <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]);
यह एक मूर्खतापूर्ण उदाहरण हो सकता है, लेकिन यह सिद्धांत मैं काम कर पाने के लिए है, जो कोणीय लिंक समारोह में इंजेक्शन निर्भरता उपयोग कर रहा है चाहते हैं।
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());
यह जिस तरह से आप यह अभी के साथ एक समस्या हो सकती है: