AngularJS Filter Function Description

Here is the filter I add in AngularJS:

angular.module('myApp', []) .filter('clean', function(){ return function(input){ return input; }; }) 

Can someone explain how I am five, why do I need an additional anonymous function to return data?

Basically why this is not working:

 angular.module('myApp', []) .filter('clean', function(input){ return input; }) 

I am trying to understand what is going on better here, so any help would be appreciated.

+4
source share
1 answer

We can just do it differently! But in what framework, standardization. Look at a service definition that works the same.

But if you look closely at the documents, it says that the .filter function and others like it should receive providers of no value . It helps:

  • Lazy or not a copy.
  • DI and all the advantages of DI, it allows you to determine the dependence on each filter.

Check out the full fiddle http://jsfiddle.net/vAHbr/4/

 angular.module('myApp', []) .filter('discountcurrency', // The provider, where we can encapsulate our filter // creation code, without having to do JavaScript ninja // stuff. As well as this is our chance to ask for // dependencies. function ($filter) { var currency = $filter('currency'); // The actual simple filter function. return function (value) { return currency(value - 5); } }); 
+3
source

Source: https://habr.com/ru/post/1493939/


All Articles