我是打字稿的新手,我正在尝试创建使用打字稿类编写的Angular.js指令,并希望在指令链接函数中使用外部角度服务,在$watch函数接收数据时调用。但是无论我怎么尝试,这个关键字总是链接到全局窗口,我无法访问注入的服务。请帮我做。这是我的指令:
module Portal.MainMenu {
class MenuScrollDirective implements ng.IDirective {
static $inject = ["$timeout", "$window"];
constructor(private $timeout: ng.ITimeoutService, private $window: ng.IWindowService) { }
restrict = "EA";
scope = {
expanded: "=",
tplChanged: "=",
fullView: "="
};
link = ($scope: ng.IScope, el: IJQSlimScroll) => {
var settings = {
position: 'left',
size: '5px'
};
init();
function init() {
$scope.$watch('expanded',(cur, prev) => {
cur && adjustScroll();
});
}
function adjustScroll() {
var winH = this.$window.innerHeight //this - refers to Window here
//but I need to access service
this.$timeout(() => {
//need access to el here
}); //same here 'this' is global
}
}
angular.module('portal.mainMenu')
.directive('menuScroll', ($timeout: ng.ITimeoutService, $window: ng.IWindowService) => {
return new MenuScrollDirective($timeout, $window);
});
}