Reputation: 35842
To define an Angular's filter, you should write:
angular.module('app', [])
.filter('mix', function () {
// Why do we return a function here?
return function (input) {
var output;
// doing some business here
return output;
};
});
Why Angular returns a function inside the callback function passed to the filter
function? Why not just use that as the placeholder and template of filter definition?
This syntax is not developer friendly at all. What limitations Angular has had to make it use this function nesting? Is it a pattern?
I guess what seems logical and normal (based on heavy use of jQuery and other libraries) is this syntax:
angular.module('app', [])
.filter('mix', function (input) {
var output;
// doing some business here
return output;
});
Upvotes: 2
Views: 121
Reputation: 48147
This all has to do with the way Angular does Dependency Injection.
You want the ability to inject services into the filter but return a function that does not employ dependency injection.
For example, let's say that your filter uses the $location
service:
angular.module('app', [])
.filter('mix', function ($location) {
// Location got injected.
// create some private functions here
function process(input) {
// do something with the input and $location
}
return function (input) {
return process(input);
};
});
You can also see from this example, doing it this way allows you to create "private" functions only available to this filter.
Upvotes: 2