0

私は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); 
      }); 
} 

答えて

0

ディレクティブリンク機能には、3番目のパラメータとしてcontroller/ctrl、4番目のパラメータとしてattributes/attrsがあります。

サンプル・リンク機能は、次のようになります。

link = ($scope: angular.IScope, el: angular.IAugmentedJQuery, ctrl: Function, attrs: angular.IAttributes). 

あなたは、コントローラの機能を呼び出すために、コントローラの参照を使用して、そこから直接あなたが操作するために必要な角度サービス機能を呼び出すことができます。

よろしくおそらく

アジャイ

0

私は一緒に活字体と角に慣れていないが、それは私には慣用的な活字のようには見えません。

class MenuScrollDirective implements ng.IDirective { 
    static $inject = ["$timeout", "$window"]; 

    constructor(private $timeout: ng.ITimeoutService, private $window: ng.IWindowService) { 
     this.restrict = "EA"; 
     this.scope = { 
      expanded: "=", 
      tplChanged: "=", 
      fullView: "=" 
     }; 
    } 

    link($scope: ng.IScope, el: IJQSlimScroll) { 
     $scope.$watch('expanded', (cur, prev) => { 
      cur && this.adjustScroll(); 
     }); 
    } 

    private adjustScroll() { 
     var winH = this.$window.innerHeight; 
     this.$timeout(() => {}); 
    } 
} 
+0

が、私はエルとDOM操作の多くを持っている:リンク機能でIJQSlimScroll、私はこのケースで何をすべきでしょうか?何らかの方法でadjustScroll関数への参照として要素を渡しますか? – sonya

+0

はい、それは私にする最もクリーンなもののように聞こえます。 –

関連する問題