DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Building Enterprise Authentication With Angular, Spring Boot, SSO, and MFA
  • Zone-Free Angular: Unlocking High-Performance Change Detection With Signals and Modern Reactivity
  • When Angular APIs Return 200 but the Frontend Is Already Failing Users
  • Why Angular Performance Problems Are Often Backend Problems

Trending

  • When Jev Says It's Sure, Is It Right? A Fraud Case Study in Neo4j
  • Idempotency Under Concurrency: Why Your Application Check Isn't the Last Line of Defense
  • You Do Not Need a PhD to Become an AI Engineer: The Essential Skill Stack
  • Why More Executors Don't Always Make Spark Faster: Understanding AQE

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  1. DZone
  2. Coding
  3. Frameworks
  4. Use Of Ngx-Bootstrap Typehead In Angular 8

Use Of Ngx-Bootstrap Typehead In Angular 8

In this article, we will learn about the Typehead component which is a cool feature of Ngx-bootstrap.

By 
Siddharth Gajbhiye
·
May. 07, 20 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
14.6K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

Introduction

Ngx-Bootstrap has released a package of open-source tools which are native Angular directives for Bootstrap 3 and 4. It contains all core components powered by Angular. In this article, we will learn about the Typehead component which is a cool feature of Ngx-bootstrap.

What Is Typeahead?

Typeahead — Also known as autocomplete or autosuggest is a language prediction tool that many search interfaces use to provide suggestions for users as they type in a textbox. This is a method for searching and filtering through text. It is also sometimes known as autocomplete, incremental search, search-as-you-type, and inline search.

Prerequisites

  • Basic knowledge of Angular
  • Visual Studio Code must be installed
  • Angular CLI must be installed
  • Node JS must be installed

Step 1

Let's create a new Angular project using the following NPM command,

TypeScript
 




x


 
1
ng new typehead-example  



Step 2 

Now, let's create a new component by using the following command,

TypeScript
 




xxxxxxxxxx
1


 
1
ng g c ngx-bootstrap-typehead  



Step 3

Install ngx-bootstrap from npm by using the folowing command.

TypeScript
 




xxxxxxxxxx
1


 
1
npm install ngx-bootstrap --save  



Or

TypeScript
 




xxxxxxxxxx
1


 
1
ng add ngx-bootstrap  --component typeahead  



This will be added to our root module

Step 4

Now let's add bootstrap styles in our index.html file .

For Bootstrap 3,

HTML
 




xxxxxxxxxx
1


 
1
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">  



For Bootstrap 4

HTML
 




xxxxxxxxxx
1


 
1
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" rel="stylesheet">  



Step 5

Let add the template in our ngx-bootstrap-typehead.component.html. 

HTML
 




xxxxxxxxxx
1


 
1
<h2>Example of Ngx-Bootstrap Typehead</h2>  
2
<div style="width: fit-content;margin: auto;margin-top: 18px;">  
3
  
4
  <pre style="background-color: antiquewhite;" class="card card-block card-header mb-3">Selected option: {{selectedOption | json}}</pre>  
5
  
6
  <input [(ngModel)]="selectedValue" [typeahead]="countries" typeaheadOptionField="name" [typeaheadScrollable]="true"  
7
    (typeaheadOnSelect)="onSelect($event)" class="form-control">  
8
  
9
</div>  



Step 6

 Now, open the ngx-bootstrap-typehead.component.ts file and add the following code in this file,

TypeScript
x
636
 
1
import { Component } from '@angular/core';  
2
import { TypeaheadMatch } from 'ngx-bootstrap/typeahead/typeahead-match.class';  
3
  
4
@Component({  
5
  selector: 'app-ngx-bootstrap-typeahead',  
6
  templateUrl: './ngx-bootstrap-typeahead.component.html'  
7
})  
8
export class NgxBootstrapTypeaheadComponent {  
9
  selectedValue: string;  
10
  selectedOption: any;  
11
  countries: any[] = [  
12
    {  
13
      "name": "Afghanistan",  
14
      "phoneCode": "+93",  
15
      "alpha2code": "AF",  
16
      "alpha3code": "AFG"  
17
    },  
18
    {  
19
      "name": "Albania",  
20
      "phoneCode": "+355",  
21
      "alpha2code": "AL",  
22
      "alpha3code": "ALB"  
23
    },  
24
    {  
25
      "name": "Algeria",  
26
      "phoneCode": "+213",  
27
      "alpha2code": "DZ",  
28
      "alpha3code": "DZA"  
29
    },  
30
    {  
31
      "name": "Montserrat",  
32
      "phoneCode": "+1",  
33
      "alpha2code": "MS",  
34
      "alpha3code": "MSR"  
35
    },  
36
    {  
37
      "name": "Morocco",  
38
      "phoneCode": "+212",  
39
      "alpha2code": "MA",  
40
      "alpha3code": "MAR"  
41
    },  
42
    {  
43
      "name": "Mozambique",  
44
      "phoneCode": "+258",  
45
      "alpha2code": "MZ",  
46
      "alpha3code": "MOZ"  
47
    },  
48
    {  
49
      "name": "Namibia",  
50
      "phoneCode": "+264",  
51
      "alpha2code": "NA",  
52
      "alpha3code": "NAM"  
53
    },  
54
    {  
55
      "name": "Nauru",  
56
      "phoneCode": "+674",  
57
      "alpha2code": "NR",  
58
      "alpha3code": "NRU"  
59
    },  
60
    {  
61
      "name": "Nepal",  
62
      "phoneCode": "+977",  
63
      "alpha2code": "NP",  
64
      "alpha3code": "NPL"  
65
    },  
66
    {  
67
      "name": "Netherlands",  
68
      "phoneCode": "+31",  
69
      "alpha2code": "NL",  
70
      "alpha3code": "NLD"  
71
    },  
72
    {  
73
      "name": "Netherlands Antilles",  
74
      "phoneCode": "+599",  
75
      "alpha2code": "AN",  
76
      "alpha3code": "ANT"  
77
    },  
78
    {  
79
      "name": "New Caledonia",  
80
      "phoneCode": "+687",  
81
      "alpha2code": "NC",  
82
      "alpha3code": "NCL"  
83
    },  
84
    {  
85
      "name": "New Zealand",  
86
      "phoneCode": "+64",  
87
      "alpha2code": "NZ",  
88
      "alpha3code": "NZL"  
89
    },  
90
     
91
    {  
92
      "name": "North Korea",  
93
      "phoneCode": "+850",  
94
      "alpha2code": "KP",  
95
      "alpha3code": "PRK"  
96
    },  
97
    {  
98
      "name": "Northern Mariana Islands",  
99
      "phoneCode": "+1",  
100
      "alpha2code": "MP",  
101
      "alpha3code": "MNP"  
102
    },  
103
    {  
104
      "name": "Norway",  
105
      "phoneCode": "+47",  
106
      "alpha2code": "NO",  
107
      "alpha3code": "NOR"  
108
    },  
109
    {  
110
      "name": "Oman",  
111
      "phoneCode": "+968",  
112
      "alpha2code": "OM",  
113
      "alpha3code": "OMN"  
114
    },     
115
  ];  
116
  
117
  onSelect(event: TypeaheadMatch): void {  
118
    this.selectedOption = event.item;  
119
  }  
120
}  


Step 7 

Now, open the app.component.html file and add the following code in the file,

HTML
 




xxxxxxxxxx
1


 
1
<app-ngx-bootstrap-typeahead></app-ngx-bootstrap-typeahead>    



Step 8 

Let's open app.module.ts file and add the following code in the file,

TypeScript
 




xxxxxxxxxx
1
25


 
1
import { BrowserModule } from '@angular/platform-browser';      
2
import { NgModule } from '@angular/core';      
3
import { FormsModule, ReactiveFormsModule } from '@angular/forms';      
4
import { AppComponent } from './app.component';      
5
import { HttpClientModule } from '@angular/common/http';       
6
import { NgxBootstrapTypeaheadComponent } from './ngx-bootstrap-typeahead/ngx-bootstrap-typeahead.component';       
7
import { TypeaheadModule } from 'ngx-bootstrap/typeahead';      
8
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';    
9
    
10
@NgModule({      
11
  declarations: [      
12
    AppComponent,      
13
    NgxBootstrapTypeaheadComponent      
14
  ],      
15
  imports: [      
16
    BrowserModule,      
17
    TypeaheadModule.forRoot(),      
18
    BrowserAnimationsModule,    
19
    FormsModule,      
20
    ReactiveFormsModule,      
21
    HttpClientModule      
22
  ],      
23
  bootstrap: [AppComponent]      
24
})      
25
export class AppModule { }  



Now it's time for the output,

ngx-bootstrap

mgx-bootstrap

ngx-bootstrap

As we can see, whenever I am typing the key, it's getting the values related to the search. We can get the data from the web API. 

Conclusion

In this article, we have seen the Ngx-Bootstrap typehead Component in an Angular 8 application.

Please give your valuable feedback/comments/questions about this article.

I hope you have enjoyed this article, as I have enjoyed writing and coding the examples. 

Please let me know how to improve it.

AngularJS

Opinions expressed by DZone contributors are their own.

Related

  • Building Enterprise Authentication With Angular, Spring Boot, SSO, and MFA
  • Zone-Free Angular: Unlocking High-Performance Change Detection With Signals and Modern Reactivity
  • When Angular APIs Return 200 but the Frontend Is Already Failing Users
  • Why Angular Performance Problems Are Often Backend Problems

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook