私はtypescriptですではかなり新しいですし、私はtypescriptですクラスを使って書かれAngular.jsディレクティブを作成しようとしていますし、ディレクティブに外部の角度サービスを使用したいディレクティブリンク機能で角度サービスにアクセスすることはできませんリンク関数は、$ watch関数が受け取ったデータを呼び出すときに呼び出されます。しかし、私が試してみても、こののキーワードは常にグローバルウィンドウにリンクしており、注入されたサービスにアクセスすることはできません。私にそれをしてください助けてください。 は、ここに私のディレクティブです:AngulaJSは
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);
});
}
が、私はエルとDOM操作の多くを持っている:リンク機能でIJQSlimScroll、私はこのケースで何をすべきでしょうか?何らかの方法でadjustScroll関数への参照として要素を渡しますか? – sonya
はい、それは私にする最もクリーンなもののように聞こえます。 –