2014-06-11 80 views
1

我正在使用typescript和angular,並且我無法在我的控制器上獲取綁定到此的函數。我應該指出,在我的情況下,控制器和$範圍是兩個不同的東西。如何綁定用作角度過濾器謂詞的函數

我試過angular.bind(this,this.filterViewedStagingItems);但那不起作用。我可以執行內聯函數並通過閉包保留$ scope,但這不是解決問題的優雅方法。

HTML:

<div class="table-entry row table-entry stagingItem" ng-repeat="stagingItem in stagingItems|filter:vm.filterViewedStagingItems"> 

代碼:

export interface IFooController extends ng.IScope { 
    vm: Controller; 
    isNewForSupplier:boolean; 
    isNewForRestaurant:boolean; 
    isPackSizeDescChanged:boolean; 
    isDescChanged:boolean; 
} 

export class Controller{ 
    public static $inject = [ 
     '$scope', 
     '$rootScope', 
     '$routeParams' 
    ]; 

    constructor(private $scope:IFooController, 
       $rootScope: ng.IScope, 
       private $routeParams:any) 
    { 
     $scope.vm = this; 
     angular.bind(this, this.filterViewedStagingItems);//Doesn't work 
    } 

    private filterViewedStagingItems(stagingItem: StagingItem): boolean 
    { 
     if (this.$scope.isNewForRestaurant && stagingItem.isNewForRestaurantOnly()) 
     { 
      return true; 
     } 

     if (this.$scope.isNewForSupplier && stagingItem.isNewItemOnly()) 
     { 
      return true; 
     } 

     if (this.$scope.isDescChanged && stagingItem.isDescriptionChangedOnly()) 
     { 
      return true; 
     } 

     if (this.$scope.isPackSizeDescChanged && stagingItem.isPackSizeChangedOnly()) 
     { 
      return true; 
     } 

     if (this.$scope.isPackSizeDescChanged && this.$scope.isDescChanged && stagingItem.isDescriptionOrPackSizeDescChangedOnly()) 
     { 
      return true; 
     } 

     if (this.areAnyFiltersEnabled()) { 
      return false; 
     } 
     return true; 
    } 

    private areAnyFiltersEnabled():boolean 
    { 
     return this.$scope.isNewForRestaurant || this.$scope.isNewForSupplier || this.$scope.isDescChanged || this.$scope.isPackSizeDescChanged 
    } 
} 

回答