How to create custom filter in AngularJs


Custom_Filter

We can create our own custom filter depending on our own requirement. Here I will show one of the simple and interesting custom filters in below example

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <meta charset="utf-8" />
    <script src="../Scripts/angular.js"></script>
    <script>
        var app = angular.module("MyApp", []);
        app.filter("reverse", function myfunction() {
            return function (input) {
                var result = "";
                 input = input || "";
                for (var i = 0; i < input.length; i++) {
                    result = input[i]+result;
                }
                return result;
            };
        });
    </script>
</head>
<body ng-app="MyApp">
    <div>
        <input type="text" name="name" ng-model="text" placeholder="Enter text" />
        <p>Input: {{text}}</p>
        <p>Filter Input :{{ text | reverse }}</p>
    </div>
</body>
</html>

Summary:
In this example we learnt to create the custom filter depending on our own requirement in angularJs.

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.