I have noticed in some search engines and websites that while I am typing the first two letters of a word, it shows me a suggested list to select. These auto complete text boxes are mostly used in real time projects to increase user interactivity with the site. Now in this post, we will look at how this is implemented in AngularJS.
For example, let's talk about the Google auto complete search box.
Here, the suggested results will come based on the previous search results or most searched results.
Implementing Auto Complete Textbox in Angular
To implement an auto complete text box in Angular, we need a angucomplete-alt directive. Angucomplete dependency should be added to the Angular Application. In this application, I am getting the data suggestion from the database table.
Steps to implement Auto complete text box are:
Step 1
Create New Project.
Go to File -> New -> Project -> ASP.NET Web Application under Web section -> Entry Application Name -> OK -> Empty template -> Checked MVC ->OK
Step 2
Add a database to the project.
Go to Solution Explorer -> Right click App_Data folder -> Add -> New item -> Select SQL Server database under Data -> Enter Database name -> Add.
Step 3
Create a table to store the data.
- Open Database ->Right click Table folder -> Add New Table -> Add Columns -> Save -> Enter table name -> OK.
- In this tutorial, I have created a table (Country) to store the country list that will appear in the auto suggestion list (This table contains CountryID and CountryName as columns).
Step 4
Add Entity Data Model.
- Go to Solution Explorer ->Right click Project name form Solution Explorer -> Add -> New item -> Select ADO.net Entity Data Model under data -> Enter model name -> Add.
- A popup window will come (Entity Data Model Wizard) -> Select Generate from database ->click Next.
- Chose your data connection -> select your database ->select Entity framework version(5.0 or 6.0) next -> select tables ->enter Model Namespace name->click Finish.
Step 5
Add Angular reference files,
- Add reference files in _Layout.cshtml page, given below. (You can download the files from angucomplete-alt.css and angucomplete-alt.js.
- Add these files in <head> tag.
- <!--Angualr refrence file for auto complete text box starts->
- <script src="~/Scripts/angular.min.js"></script>
- <script src = "~/Scripts/angular-route.min.js" > < /script>
- <script src = "~/Scripts/angucomplete-alt.js" > < /script>
- <link href = "~/Content/angucomplete-alt.css" rel = "stylesheet" / >
- <script src = "~/Scripts/app.js" > < /script>
- <!--Angualr refrence file for auto complete text box starts->
Step 6
Add Controller.
- Add Controller by right clicking on Controllers folder -> Add -> Controller ->name it as Home Controller.
- Replace the code with the code, given below:
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.Mvc;
- namespace AngularAutoCompleteTextbox.Controllers
- {
- public class HomeController: Controller
- {
- public ActionResult Index()
- {
- return View();
- }
- public ActionResult getAllCountries()
- {
- using(DatabaseEntities db = new DatabaseEntities())
- {
- var countrylist = db.Countries.OrderBy(a => a.CountryName)
- .ToList();
- return new JsonResult
- {
- Data = countrylist, JsonRequestBehavior = JsonRequestBehavior.AllowGet
- };
- }
- }
- }
- }
- Index action is used to render the view.
- getAllCountries method gets the data from the database and sends it to the Angular Controller in JSON format.

GeeksPosted May 7, 2017, 2:47 PM
Hey, I faced the same problem with angucomplete laibrary... but solve so you can follow these steps or download from here http://www.geeksblood.com/autocomplete-in-angularjs/
Nilesh SonkusarePosted Mar 3, 2017, 2:14 PM
Please help me.When i search second time it will not display any selected party object. Please see my html & angular js code. <div angucomplete-alt id="txtCustomerName" placeholder="Search customer name" selected-object="SelectedParty" local-data="Parties" search-fields="PartyName" title-field="PartyName" minlength="1" input-class="form-control" match-class="highlight" text-searching="Searching..." text-no-results="No result found"> </div> $scope.SelectedParty = function (selected) { if (selected) { $scope.PartyAddress = ""; $scope.SelectedParty = selected.originalObject; } else { console.log('cleared'); } };
rajbir thindPosted Nov 26, 2016, 9:08 AM
Dear Sir if we already using any controller and module in angular then it show me error
Former memberPosted Jul 1, 2016, 5:22 AM
Nice one
Vignesh ManiPosted Jun 26, 2016, 4:35 PM
Nice one
Santhakumar MunuswamyPosted Jun 25, 2016, 2:37 AM
Thank you for nice article . Keep it up