Overview
In this article, we will be looking at the search functionality in AngularJS. Here, the requirement is to search name, address, and so on, to display the corresponding results or rows.
For more articles on AngularJS, kindly refer to
- Overview Of AngularJS
- Modules And Controller In AngularJS
- Controllers in AngularJS
- AngularJS ng src Directive
- AngularJS ng-Repeat Directive
- Handling Events In AngularJS
- How To Use Two-Way Data Binding In AngularJS
- Filters In AngularJS
Introduction As our requirement is to implement search functionality and display various details or display corresponding rows, here is our model.
var mypartone = angular.module("mymodule", []).controller("myController", function($scope) {
var employees = [
{ name: "Akshay", dateOfBirth: new Date("July 21, 1960"), Address: "Mumbai", Salary: "1234.34" },
{ name: "Milind", dateOfBirth: new Date("July 20, 1960"), Address: "Nashik", Salary: "12.34" },
{ name: "Raghvan", dateOfBirth: new Date("July 19, 1960"), Address: "Pune", Salary: "1235.34" },
{ name: "Hari", dateOfBirth: new Date("July 18, 1960"), Address: "Mumbai", Salary: "5589.34" }
];
$scope.employees = employees;
});
Here, our model contains an array in which we display the records containing name, DOB, address, and Salary. We attach it to that $scope object. Our View contains names and various details. We are binding those elements with binding expression and using the ng-repeat directive to loop through those records. So, our View is
<table>
<thead>
<tr>
<th> Name </th>
<th>Date of Birth </th>
<th>Address </th>
<th>Salary </th>
</tr>
</thead>
<tbody>
<tr ng-repeat="employee in employees">
<td>{{employee.name }}</td>
<td>{{employee.dateOfBirth }}</td>
<td>{{employee.Address }}</td>
<td>{{employee.Salary }}</td>
<%-- <td>{{employee.Salary| currency }}</td>--%>
</tr>
</tbody>
</table>
And, our output is

Now, we will add a textbox above that, by using an input tag. Then, we will be adding an ng-model directive to it. Search By
<input type="text" placeholder"Search By" ng-model="SearchText" />
Now, you can see that we don’t have that property SearchText. In our model, we will be displaying those dynamically and attaching those in the $scope object as well. When you reload the page, you will get the following.

So, at the moment, search functionality is not implemented. We want to search by various fields, like address, etc. So, we are going to use a filter in the ng-repeat directive, and in that, we will pass that ng-model to it, as shown below.
<tr ng-repeat="employee in employees|filter :SearchText">
Now, when you reload the page, just type what you want.










Ravi KandelPosted Aug 14, 2016, 3:18 AM
Nice
Vignesh ManiPosted Jul 30, 2016, 8:13 PM
Good one
Sthitaprajnya Debasis NayakPosted Jul 30, 2016, 1:41 PM
Nice Article !!!
Debasis SahaPosted Jul 30, 2016, 2:15 AM
Good One..