Use AngularJS to create a custom filter, angularjs Filter

Source: Internet
Author: User

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:
 
 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.