How to implement a search box in Angular

0 votes

How to implement a search box in Angular?

I'm trying to implement a search box in Angular that filters a list of items dynamically as the user types, but I'm unsure of the best approach. I’ve seen mentions of using two-way binding with ngModel and employing pipes for filtering, but I’m not clear on how to tie everything together effectively. Could someone explain how to create a functional search box in Angular?

Dec 4, 2024 in Web Development by Nidhi
• 9,600 points
104 views

1 answer to this question.

0 votes

1. Create an Angular Component

ng generate component search

2. App Component (app.component.ts)

Define the data and the search term, and filter the data based on the search term:

import { Component } from '@angular/core';

@Component({

  selector: 'app-root',

  templateUrl: './app.component.html',

  styleUrls: ['./app.component.css']

})

export class AppComponent {

  searchTerm: string = '';  // Holds the search term

  data: string[] = [

    'Angular Tutorial', 'React Tutorial', 'Vue Tutorial', 

    'Svelte Tutorial', 'JavaScript Basics', 'TypeScript Basics'

  ];

  // Filter the data based on the search term

  get filteredData() {

    return this.data.filter(item => item.toLowerCase().includes(this.searchTerm.toLowerCase()));

  }

}

3. App Component Template (app.component.html)

Create an input field for the search and display filtered results:

<div>

  <input type="text" [(ngModel)]="searchTerm" placeholder="Search..." />

  <ul>

    <li *ngFor="let item of filteredData">{{ item }}</li>

  </ul>

</div>

4. App Module (app.module.ts)

Import FormsModule to enable two-way data binding with ngModel:

import { NgModule } from '@angular/core';

import { BrowserModule } from '@angular/platform-browser';

import { FormsModule } from '@angular/forms';  // Import FormsModule

import { AppComponent } from './app.component';

@NgModule({

  declarations: [AppComponent],

  imports: [BrowserModule, FormsModule],  // Include FormsModule

  providers: [],

  bootstrap: [AppComponent]

})

export class AppModule { }

5. Run the Application

ng serve

answered Dec 4, 2024 by Navya

Related Questions In Web Development

0 votes
1 answer

How to update Angular version in a project?

Angular is a powerful framework for building ...READ MORE

answered Nov 4, 2024 in Web Development by kavya
223 views
0 votes
1 answer

How to create a service file in Angular?

To create a service file in Angular, ...READ MORE

answered Nov 13, 2024 in Web Development by kavya
109 views
0 votes
1 answer

How to create an Observable from a string in Angular 2?

In Angular (or any JavaScript application using ...READ MORE

answered Nov 27, 2024 in Web Development by kavya
110 views
0 votes
1 answer

How to import a JSON file in Angular?

Step 1: Create a JSON File Put your ...READ MORE

answered Dec 13, 2024 in Web Development by Navya
70 views
+1 vote
8 answers

How can I implement process.env in Angular 5 environment?

Users do not have access to process.env ...READ MORE

answered Apr 3, 2018 in DevOps & Agile by DareDev
• 6,890 points
13,424 views
0 votes
1 answer
0 votes
4 answers

ReactJS vs Angular Comparison: Which is better?

Parameters React Angular Type React is a JavaScript library, and it ...READ MORE

answered Jan 7, 2021 in Events & Trending Topics by Focusteck
• 140 points
2,019 views
+4 votes
9 answers

***IMPORTANT*** AngularJS Interview Questions.

Yes, I agree with Omkar AngularJs is ...READ MORE

answered Mar 17, 2019 in Career Counselling by Sharad
• 180 points
3,900 views
0 votes
1 answer

How to implement a debounce time in keyup event in Angular 6

To implement a debounce time in keyup ...READ MORE

answered Oct 25, 2024 in Web Development by kavya
162 views
0 votes
1 answer

How to open a modal popup on button click in Angular 8?

1. Install Angular Material (Optional) If you want ...READ MORE

answered Dec 13, 2024 in Web Development by Navya
64 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP