Tuesday, June 30, 2015

AngularJS filter

AngularJS filter, is a special type of filter which can be used to filter out the contents of an array/collection bound to an ng-repeat directive, this comes in handy when we need to implement dynamic search/filtering capability on an array/collection of elements

The following is the syntax for using AngularJS filter
ng-repeat="<array/collection> | filter:<filter text>"

The following is an example of using filter, filter out specific elements from an array of objects.

<html ng-app>
<head>
    <meta charset="utf-8" />
    <title>AngularJS - Basic</title>
    <script src="angular.min.js"></script>
</head>
<body>
          <div ng-init="Employees=[
                                      {id:1,Name:'Tom'},
                                      {id:2,Name:'Harry'},
                                      {id:3,Name:'Peter'},
                                      {id:4,Name:'Thomas'},
                                      {id:5,Name:'Tiger'},
                                      {id:6,Name:'Topas'}]">
                    <ul>
                       <li ng-repeat="emp in Employees | filter:'To'">
                         {{ emp.id }} - {{ emp.Name }}<br/>
                       </li>
                   </ul>
          </div>
</body>
</html>

Output:


Search Flipkart Products:
Flipkart.com

No comments: