Simple Filter Action With AngularJS

In this article we will see how to implement a filter functionality using AngularJS. If you are a beginner to AngularJS then just go through these links:
modules
, directives, data binding.

Step 1

In the controller add JavaScript logic for model data. In this example we hardcoded the JSON data as specified below.

Filter AngularJS1

Step 2

As parts of the controller definition, just add this data as a $scope module:

Filter AngularJS2

Step 3

In the view page, just start with the input control to enter the search keyword, and assign the search text to AngularJS model “q”.

Filter AngularJS3

Step 4

Before we show the search results, we need to render some data using the “ng-repeat “ directive and bind the model values, like id and body text as part of the data view.

Filter AngularJS4

Step 5

We will just check to render the output image as expected; the controller data will render in the page.

Filter AngularJS5

Step 6

Just type some text to filter:

Filter AngularJS6
This will filter automatically because AngularJS will do the filter as we mentioned “filter:q” as part of “ng-repeat”.

Summary

An AngularJS “filter” selects a subset of items from an array and returns it as a new array. It’s really easy to add filter functionality. 


Similar Articles