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
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Because the DevOps movement has redefined engineering responsibilities, SREs now have to become stewards of observability strategy.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Related

  • Rediscovering Angular: Modern Advantages and Technical Stack
  • Angular Input/Output Signals: The New Component Communication
  • Azure Deployment Using FileZilla
  • Angular RxJS Unleashed: Supercharge Your App With Reactive Operators

Trending

  • Implementing Explainable AI in CRM Using Stream Processing
  • Designing a Java Connector for Software Integrations
  • How To Build Resilient Microservices Using Circuit Breakers and Retries: A Developer’s Guide To Surviving
  • Securing the Future: Best Practices for Privacy and Data Governance in LLMOps
  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 user avatar
Siddharth Gajbhiye
·
May. 07, 20 · Tutorial
Likes (3)
Comment
Save
Tweet
Share
14.2K Views

Join the DZone community and get the full member experience.

Join For Free

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

  • Rediscovering Angular: Modern Advantages and Technical Stack
  • Angular Input/Output Signals: The New Component Communication
  • Azure Deployment Using FileZilla
  • Angular RxJS Unleashed: Supercharge Your App With Reactive Operators

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

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 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!