0

で$フィルターを注入することができません。私は私のディレクティブ内のコントローラを使用しています。内部フィルタを使用しますが、私は「この$フィルタは関数ではありません。」実行時例外を取得していたコントローラのコンストラクタに:私は、関数(文字列)の変化(searchAttribute)を宣言しました。角度活字体:コントローラ

私は$フィルタサービス同様注入Googleで多くの例を持っているが、私はそれは私のために働いていない理由を把握することができません。

マイコード:

module app.directive { 

interface MyDirectiveScope extends ng.IScope { 
    myModel: any[]; 
    change(searchAttribute: string); 
} 

class MyController { 
    static $inject = ['$scope', '$filter']; 

    constructor(public $scope: MyDirectiveScope, public $filter: ng.IFilterService) { 
     //code 

     $scope.change = function (searchAttribute) { 
      $scope.myModel = this.$filter('filter')($scope.myModel, searchAttribute); //error : this.$filter turns out to be undefined here 
     }; 
    } 
} 

class MyDirective implements ng.IDirective { 

    static instance(): ng.IDirective { 
     return new MyDirective; 
    } 

    restrict = "E"; 
    replace = true; 
    templateUrl = "myTemplate.html"; 
    controller = MyController; 
    scope = {}; 
} 


angular.module("myModule") 
    .directive("myDirective", MyDirective.instance);} 

答えて

1

あなたは、参照コンストラクタのためthisを使用しようとしています。しかし、別の関数の中では、thisはコンストラクタではなく変更関数への参照です。先にselfのような別の変数にポイントコントラクターが必要です。

コードに行くことができます:

constructor(public $scope: MyDirectiveScope, public $filter: ng.IFilterService) { 
    let self = this; 

    $scope.change = function (searchAttribute) { 
     $scope.myModel = self.$filter('filter')($scope.myModel, searchAttribute); 
    }; 
} 
1

あなたは上記の関数の内部クラスのインスタンスのコンテキストとしてthisを持っているTypescript arrow functionsを使用することができます。

$scope.change = (searchAttribute) => { 
    $scope.myModel = this.$filter('filter')($scope.myModel, searchAttribute); 
    // Here, this refers to your class instance context 
}; 
関連する問題