Use AngularJS to create a custom filter, angularjs Filter
Angularjs filter is one of the great features of angularjs. One day, you may need to use a custom filter. Fortunately, you have found this blog post.
The following shows what the custom filter looks like (note myfilter ):
Our custom filter is called "myfilter", which has four parameters separated.
This is a sample input that will be used:
$scope.friends = [{name:'John', phone:'555-1276'}, {name:'Annie', phone:'800-BIG-MARY'}, {name:'Mike', phone:'555-4321'}, {name:'Adam', phone:'555-5678'}, {name:'David', phone:'555-8765'}, {name:'Mikay', phone:'555-5678'}];
The filter only shows the phone number containing "555", which is the sample output:
Name Phone John 555-1276 Mike 555-4321 Adam 555-5678 David 555-8765 Mikay 555-5678
The processing flow for filtering "555" is executed by "windowScopedFilter", which is the fourth parameter of the filter 'myfilter.
The following describes how to implement these functions (add logging to each input parameter ):
var myapp = angular.module('MyFilterApp', []); myapp.filter('myfilter', function() { return function(input, param1) { console.log("------------------------------------------------- begin dump of custom parameters"); console.log("input=",input); console.log("param1(string)=", param1); var args = Array.prototype.slice.call(arguments); console.log("arguments=", args.length); if (3<=args.length) { console.log("param2(string)=", args[2]); } if (4<=args.length) { console.log("param3(bool)=", args[3]); } console.log("------------------------------------------------- end dump of custom parameters"); // filter if (5<=args.length) { return window[args[4]](input); } return input; }; });
Most of the above Code is logged (the Translator's note: display the information to the console). The most important part of filtering is:
// filter if (5<=args.length) { return window[args[4]](input); } return input;
"Return window [args [4] (input)" calls the fourth parameter, which is 'windowscopedfilter '.
This is the console output:
"------------------------------------------------- begin dump of custom parameters" custom_filter_function.html:21 "input=" [object Array] custom_filter_function.html:22 "param1(string)=" "param1" custom_filter_function.html:23 "arguments=" 5 custom_filter_function.html:25 "param2(string)=" "param2" custom_filter_function.html:27 "param3(bool)=" true custom_filter_function.html:30 "------------------------------------------------- end dump of custom parameters" custom_filter_function.html:32 "------------------------------------------------- begin dump of custom parameters" custom_filter_function.html:21 "input=" [object Array] custom_filter_function.html:22 "param1(string)=" "param1" custom_filter_function.html:23 "arguments=" 5 custom_filter_function.html:25 "param2(string)=" "param2" custom_filter_function.html:27 "param3(bool)=" true custom_filter_function.html:30 "------------------------------------------------- end dump of custom parameters" custom_filter_function.html:32
Complete code: