How to Filter an Array based on user input in AngularJS ?
Last Updated :
28 Apr, 2025
AngularJS is a JavaScript-based framework. It can be used by adding it to an HTML page using a <script> tag. AngularJS helps in extending the HTML attributes with the help of directives and binding of data to the HTML with expressions. Angular filters can be added in AngularJS to format data.
In this article, we will be discussing how to filter an array based on user input in AngularJS. Filtering in angular works by typing a letter in the input field and the list will shrink or grow based on the matched results.
Filters in Angular: AngularJS provides users to use filters to transform data like 'currency' which formats a number to a currency format, 'date' which formats a date to a specified format, etc. Filters in angular can be added to an expression or directives using the pipe | symbol as shown below.
{{ expression | filterName:parameter }}
To filter an array of data based on the user's input, we use the 'ng-model' directive by setting it on an input field. After which, we can use the value of the input field as an expression in the filter. So, to filter an array, all we need is an array of data and an input field to collect the user input.
The ng-model directive binds the values of the HTML controls like input, select, etc, and stores the required user value in a variable that can be used whenever we require that value. It is mostly supported by <input>, <select>, and <textarea>. For creating the filter to search for an array of data, we'll be using this directive of angular.
Syntax:
<div ng-app="myApp" ng-controller="namesCtrl">
<p>
<input type="text" ng-model="test">
</p>
<ul>
<li ng-repeat="letter in names | filter : test">
{{ letter }}
</li>
</ul>
</div>
Example 1: The below example demonstrates how to filter an array based on the user input in HTML. When we click in the Input field and try typing any letter in the input field, we will see that the list will go shrinking if the match results of the typed letter get matched.
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>GeeksforGeeks</title>
<script src=
"https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js">
</script>
</head>
<body>
<div style="text-align: center">
<h1 style="color: green">GeeksforGeeks</h1>
<h3>Filtering an Array in AngularJS</h3>
</div>
<div>
<div ng-app="gfgApp"
ng-controller="langsCtrl">
<p>Search any word in input field:</p>
<p>
<input type="text" ng-model="test" />
</p>
<ul>
<li ng-repeat=
"word in lang | filter:test">
{{ word }}
</li>
</ul>
</div>
<script>
angular.module("gfgApp", []).controller("langsCtrl",
function ($scope) {
$scope.lang = ["Java","C++","Python",
"Go","C","SQL","JavaScript",
];
});
</script>
</div>
</body>
</html>
Output:
 Example 2: The below example demonstrates a customized array-based filtration on the user input in HTML, where the list of arrays is customized and filtered based on the user input.
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>GeeksforGeeks</title>
<script src=
"https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js">
</script>
</head>
<body>
<div style="text-align: center">
<h1 style="color: green">GeeksforGeeks</h1>
<h3>Filtering an Array in AngularJS</h3>
</div>
<div>
<div ng-app="gfgApp" ng-controller="langsCtrl">
<p>Search any word in input field:</p>
<p>
<input type="text" ng-model="test" />
</p>
<ul>
<li ng-repeat="word in lang | filter:test"
style="background-color: lightgreen;
width: 5rem; padding: 2px">
{{ word }}
</li>
</ul>
</div>
<script>
angular.module("gfgApp", []).controller("langsCtrl",
function ($scope) {
$scope.lang = ["Java","C++","Python","Go",
"C","SQL","JavaScript"];
});
</script>
</div>
</body>
</html>
Output:
Â
Similar Reads
HTML Tutorial HTML stands for HyperText Markup Language. It is the standard language used to create and structure content on the web. It tells the web browser how to display text, links, images, and other forms of multimedia on a webpage. HTML sets up the basic structure of a website, and then CSS and JavaScript
11 min read
AVL Tree Data Structure An AVL tree defined as a self-balancing Binary Search Tree (BST) where the difference between heights of left and right subtrees for any node cannot be more than one. Balance Factor = left subtree height - right subtree heightFor a Balanced Tree(for every node): -1 ⤠Balance Factor ⤠1Example of an
4 min read
HTML Interview Questions and Answers HTML (HyperText Markup Language) is the foundational language for creating web pages and web applications. Whether you're a fresher or an experienced professional, preparing for an HTML interview requires a solid understanding of both basic and advanced concepts. Below is a curated list of 50+ HTML
14 min read
HTML Introduction HTML stands for Hyper Text Markup Language, which is the core language used to structure content on the web. It organizes text, images, links, and media using tags and elements that browsers can interpret. As of 2025, over 95% of websites rely on HTML alongside CSS and JavaScript, making it a fundam
6 min read
Python Match Case Statement Introduced in Python 3.10, the match case statement offers a powerful mechanism for pattern matching in Python. It allows us to perform more expressive and readable conditional checks. Unlike traditional if-elif-else chains, which can become unwieldy with complex conditions, the match-case statement
7 min read
HTML Tags - A to Z List HTML Tags are fundamental elements used to structure and format content on web pages. They provide instructions to web browsers on how to render text, images, links, and other media.HTML tags are enclosed in angle brackets < > and usually come in pairs: an opening tag and a closing tag. The cl
15+ min read
30+ Web Development Projects with Source Code [2025] Web development is one of the most in-demand career paths in the IT industry, experiencing consistent growth of around 20â25% annually. Whether you're a student starting out or an experienced professional looking to switch or advance your career, it's essential to go beyond theory and demonstrate yo
4 min read
Frontend Developer Interview Questions and Answers Frontend development is an important part of web applications, and it is used to build dynamic and user-friendly web applications with an interactive user interface (UI). Many companies are hiring skilled Frontend developers with expertise in HTML, CSS, JavaScript, and modern frameworks and librarie
15+ min read
HTML DOCTYPE Declaration HTML DOCTYPE (Document Type Declaration) is an instruction that appears at the beginning of an HTML document, before the <html> tag.Its primary role is to tell the web browser which version of HTML the page is written in, ensuring that the browser renders the content correctly. It is not an HT
4 min read
HTML Forms HTML forms, defined using the <form> Tags are essential for collecting user input on web pages. They incorporate a variety of interactive controls such as text fields, numeric inputs, email fields, password fields, checkboxes, radio buttons, and submit buttons. Over 85% of websites rely on for
5 min read