Open In App
Related Articles

Angular ngx Bootstrap Typeahead Component

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

Angular ngx bootstrap is a bootstrap framework used with angular to create components with great styling and this framework is very easy to use and is used to make responsive websites. In this article, we will know how to use Typeahead in angular ngx bootstrap.

Installation syntax:

npm install ngx-bootstrap --save 

Approach:

  • First, install the angular ngx bootstrap using the above-mentioned command.
  • add the following script in index.html
<link href=”https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css” rel=”stylesheet”>
  • Import typeahead component in module.ts
  • In app.component.html make a typeahead component.
  • Serve the app using ng serve.
 

Example 1:

index.html

<!doctype html>
<html lang="en">
  
<head>
    <meta charset="utf-8">
    <title>Demo</title>
    <base href="/">
    <meta name="viewport" content=
        "width=device-width, initial-scale=1">
    <link href=
        rel="stylesheet">
  
    <link rel="icon" type="image/x-icon" 
        href="favicon.ico">
    <link rel="preconnect" 
        href="https://fonts.gstatic.com">
    <link href=
        rel="stylesheet">
    <link href=
        rel="stylesheet">
</head>
  
<body class="mat-typography">
    <app-root></app-root>
</body>
  
</html>

                    

app.component.html

<pre id='gfg1' class="card 
    card-block card-header mb-3">
    Model: {{gfg | json}}
</pre>
  
<input [(ngModel)]="gfg" 
    [typeahead]="geeks" 
    class="form-control">

                    

app.module.ts

import { NgModule } from '@angular/core';
  
// Importing forms module
import { FormsModule,
ReactiveFormsModule  } 
from '@angular/forms';
import { BrowserModule } 
from '@angular/platform-browser';
import { BrowserAnimationsModule } 
from '@angular/platform-browser/animations';
import { TypeaheadModule} 
from 'ngx-bootstrap/typeahead';
  
   
import { AppComponent } 
from './app.component';
   
@NgModule({
  bootstrap: [
    AppComponent
  ],
  declarations: [
    AppComponent
  ],
  imports: [
    FormsModule,
    BrowserModule,
    BrowserAnimationsModule,
    ReactiveFormsModule,
    TypeaheadModule.forRoot()
      
  ]
})
export class AppModule { }

                    

app.component.css

#gfg1 {
    margin: 10px;
}

                    

app.component.ts

import { Component, OnInit, LOCALE_ID } 
from '@angular/core';
  
@Component({
    selector: 'app-root',
    templateUrl: './app.component.html',
    styleUrls: ['./app.component.css']
})
export class AppComponent {
    'gfg': string;
    geeks: string[] = [
        'HTML',
        'JavaScript',
        'Java',
        'AngularJS',
        'ReactJS',
        'Bootstrap'
    ];
}

                    

Output:



Last Updated : 06 Jan, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads