Access scope variables from a filter in AngularJS

Sergei Basharov

I am passing date value to my custom filter this way:

angular.module('myapp').
  filter('filterReceiptsForDate', function () {
    return function (input, date) {
      var out = _.filter(input, function (item) {
        return moment(item.value.created).format('YYYY-MM-DD') == date;
      });
      return out;
    }
  });

I would like to inject a couple of scope variables there too, like what I can do in directives. Is that possible to do this without having to passing these vars explicitly as function arguments?

Stewie

Apparently you can.

Usually you would pass scope variables to the filter as function parameter:

function MyCtrl($scope){
  $scope.currentDate = new Date();
  $scope.dateFormat = 'short';
}
<span ng-controller="MyCtrl">{{currentDate | date:dateFormat}}</span> // --> 7/11/13 4:57 PM

But, to pass the current scope in, you'd have to pass this:

<span ng-controller="MyCtrl">{{currentDate | date:this}}</span>

and this will be a reference to current scope:

Simplified:

app.controller('AppController',
    function($scope) {
      $scope.var1 = 'This is some text.';
      $scope.var2 = 'And this is appended with custom filter.';
    }
  );
  

app.filter('filterReceiptsForDate', function () {
  return function (input, scope) {
    return input + ' <strong>' + scope.var2 + '</strong>';
  };
});
<div ng-bind-html-unsafe="var1 | filterReceiptsForDate:this"></div>
<!-- Results in: "This is some text. <strong>And this is appended with custom filter.</strong>" -->

PLUNKER

Warning:

  1. Be careful with this and use scope only to read the values inside the filter, because otherwise you will easily find your self in $digest loop.
  2. Filters that require such a "heavy" dependency (the whole scope) tend to be very difficult to test.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

AngularJS - Access $scope from controller in custom filter

From Dev

access $scope from $rootscope angularjs

From Dev

Changing AngularJS scope variables from an inside function

From Dev

Access variables from the global scope within a closure

From Dev

Access variables from the global scope within a closure

From Java

AngularJS access parent scope from child controller

From Dev

AngularJS access controller $scope from outside

From Dev

Angularjs - How to access scope from $routeChangeSuccess?

From Dev

How to pass non-scope variables to AngularJS filter?

From Dev

AngularJS - How to access $scope from a directive with specified scope

From Dev

AngularJS Directive Binding link: has no access to scope variables

From Dev

AngularJS Directive Binding link: has no access to scope variables

From Dev

Inject $scope into filter (AngularJS)

From Dev

AngularJS: Scope Filter Not Working

From Dev

AngularJs scope variables in console

From Dev

Testing AngularJS scope variables

From Dev

AngularJS Factories - Access private variables from methods

From Dev

AngularJS filter ng-repeat based on value from scope

From Dev

AngularJS Access $scope in function

From Dev

Access to custom filter from an Angular directive with isolate scope

From Dev

How do I access outer scope variables from a callback in Angular?

From Dev

AngularJS : How to access scope from ui-grid cell template?

From Dev

How to access angularjs root scope from chrome extension

From Dev

What would prevent access to AngularJS $scope from browser console?

From Dev

AngularJS: accessing scope variables in $routeProvider

From Dev

Angularjs $scope variables not updating in view

From Dev

Angularjs $scope variables not updating in view

From Dev

AngularJS: accessing scope variables in $routeProvider

From Java

How to access parent scope from within a custom directive *with own scope* in AngularJS?

Related Related

  1. 1

    AngularJS - Access $scope from controller in custom filter

  2. 2

    access $scope from $rootscope angularjs

  3. 3

    Changing AngularJS scope variables from an inside function

  4. 4

    Access variables from the global scope within a closure

  5. 5

    Access variables from the global scope within a closure

  6. 6

    AngularJS access parent scope from child controller

  7. 7

    AngularJS access controller $scope from outside

  8. 8

    Angularjs - How to access scope from $routeChangeSuccess?

  9. 9

    How to pass non-scope variables to AngularJS filter?

  10. 10

    AngularJS - How to access $scope from a directive with specified scope

  11. 11

    AngularJS Directive Binding link: has no access to scope variables

  12. 12

    AngularJS Directive Binding link: has no access to scope variables

  13. 13

    Inject $scope into filter (AngularJS)

  14. 14

    AngularJS: Scope Filter Not Working

  15. 15

    AngularJs scope variables in console

  16. 16

    Testing AngularJS scope variables

  17. 17

    AngularJS Factories - Access private variables from methods

  18. 18

    AngularJS filter ng-repeat based on value from scope

  19. 19

    AngularJS Access $scope in function

  20. 20

    Access to custom filter from an Angular directive with isolate scope

  21. 21

    How do I access outer scope variables from a callback in Angular?

  22. 22

    AngularJS : How to access scope from ui-grid cell template?

  23. 23

    How to access angularjs root scope from chrome extension

  24. 24

    What would prevent access to AngularJS $scope from browser console?

  25. 25

    AngularJS: accessing scope variables in $routeProvider

  26. 26

    Angularjs $scope variables not updating in view

  27. 27

    Angularjs $scope variables not updating in view

  28. 28

    AngularJS: accessing scope variables in $routeProvider

  29. 29

    How to access parent scope from within a custom directive *with own scope* in AngularJS?

HotTag

Archive