How to create a custom filter using AngularJS

Source: Internet
Author: User

How to create a custom filter using AngularJS

This article mainly introduces how to use AngularJS to create custom filters. AngularJS is a very popular JavaScript library. For more information, see

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:

?

1

2

3

4

5

6

$ Scope. friends = [{name: 'john', phone: '2017-555 '},

{Name: 'Annie ', phone: '2017-BIG-MARY '},

{Name: 'Mike ', phone: '2017-555 '},

{Name: 'Adam ', phone: '2017-555 '},

{Name: 'David', phone: '2017-555 '},

{Name: 'mikay ', phone: '2017-555'}];

The filter only shows the phone number containing "555", which is the sample output:

?

1

2

3

4

5

6

Name Phone

John 555-1276

Mike June 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 ):

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

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:

?

1

2

3

4

5

// 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:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

"----------------------------------------------- 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:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

<Html>

<Head>

<Script src = "angular. min. js"> </script>

<Script type = "text/javascript">

Function windowScopedFilter (input ){

Var output = [];

Angular. forEach (input, function (v, k ){

If (v. phone. contains ("555 ")){

Output. push (v );

}

});

Return output;

}

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;

};

});

Myapp. controller ('myfiltercontroller', ['$ scope', function ($ scope ){

$ Scope. friends = [{name: 'john', phone: '2017-555 '},

{Name: 'Annie ', phone: '2017-BIG-MARY '},

{Name: 'Mike ', phone: '2017-555 '},

{Name: 'Adam ', phone: '2017-555 '},

{Name: 'David', phone: '2017-555 '},

{Name: 'mikay ', phone: '2017-555'}];

}]);

</Script>

</Head>

<Body ng-app = "MyFilterApp">

<Div ng-controller = "MyFilterController">

<Table id = "searchTextResults">

<Tr> <th> Name </th> <th> Phone </th> </tr>

<Tr ng-repeat = "friend in friends | myfilter: 'param1': 'param2': true: 'windowscopedfilter'">

<Td >{{ friend. name }}</td>

<Td >{{ friend. phone }}</td>

</Tr>

</Table>

</Div>

<Hr>

</Body>

</Html>

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.