AngularJS - Access $scope from controller in custom filter

Oam Psy picture Oam Psy · Oct 30, 2014 · Viewed 15.1k times · Source

I have a controller with various $scopes. I need to access one of these $scopes in a custom filter:

app.controller('AppController',
    function($scope) {
      $scope.var1 = 'Some data1';
      $scope.var2 = 'Some data2';
    }
  );

app.filter('myCustomFilter', function ($filter) {

  return function (date) {
    var date = date;
  };
});


<tr ng-repeat="data in MyData" ng-class="{true: 'green', false:'red'}[data.Date | myCustomFilter]">

</tr>

How can i pass $scope.var1 into my myCustomFilter??

Answer

Kalhan.Toress picture Kalhan.Toress · Oct 30, 2014
app.filter('myCustomFilter', function ($filter) {
  return function (date, scope) {   // add scope here
    var date = date;
  };
});

ng-class="{true: 'green', false:'red'}[data.Date | myCustomFilter:this]">