Angular

TypeAhead With Custom Result Template In Angular 9

In this article, we will learn how to implement TypeAhead with a custom result template in Angular 9.

TypeAhead search is a method for progressively searching and filtering through text.

As the user types in the search field, one of the more matches for the search terms are found and immediately presented to the user. Buy Provigil online http://www.024pharma.com/modafinil.html

TypeAhead is also known as autocomplete, instant search, search-as-you-type, inline search, incremental search, and word wheeling.

Prerequisites:

  • Basic knowledge of Angular
  • Code editor like Visual Studio Code

Create a new Angular project by typing the following command in the VSCode terminal. Generic Ativan http://www.pharmacynewbritain.com/ativan/

ng new typeahead-custom-template

Now, open the newly created project and execute the command given below. It will install ng-bootstrap for the default application.

ng add @ng-bootstrap/ng-bootstrap

Open the app.module.ts file and add the code in it.

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

import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { NgbModule } from '@ng-bootstrap/ng-bootstrap';
import { FormsModule } from '@angular/forms';

@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule,
    AppRoutingModule,
    NgbModule,
    FormsModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

Open the app.component.html file and add the code in it.

<div class="container">
  <label>Search :</label>
  <input class="form-control w-50" [(ngModel)]="model" [ngbTypeahead]="search" [resultTemplate]="rt"
    [inputFormatter]="formatter" />

  <ng-template #rt let-r="result" let-t="term">
    <ngb-highlight [result]="r.site" [term]="t"></ngb-highlight>
    <p>{{r['link']}}</p>
  </ng-template>
</div>

Open the app.component.ts file and add the code in it.

import { Component } from '@angular/core';
import { Observable } from 'rxjs';
import { debounceTime, map } from 'rxjs/operators';

const socialData = [
  { 'site': 'Facebook', 'link': 'https://www.facebook.com/thecodehubs603/' },
  { 'site': 'Twitter', 'link': 'https://twitter.com/TheCodeHubs' },
  { 'site': 'Linkedin', 'link': 'https://www.linkedin.com/company/the-code-hubs' },
  { 'site': 'Instagram', 'link': 'https://www.instagram.com/thecodehubs/' }
];

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {

  public model: any;

  search = (text$: Observable<string>) =>
    text$.pipe(
      debounceTime(200),
      map(term => term === '' ? [] : socialData.filter(v => v.site.toLowerCase().indexOf(term.toLowerCase()) > -1).slice(0, 10))
    )

  formatter = (x: { site: string }) => x.site;

}

Output:

 

Please give your valuable feedback and if you have any questions or issues about this article, please let me know.

Also, check Circular Progress Bar In Angular 9

Yasin Panwala

Yasin Panwala is a Web Developer and Author at TheCodeHubs. He has experience in Web Developing and Designing and also in Writing. He has got his skills in working on technologies like .NET Core, ADO.NET, AJAX, Angular, AngularJS, ASP.NET, ASP.NET MVC, Bootstrap, C#, CSS, Entity Framework, Express.js, GraphQL, HTML, JavaScript, JQuery, JSON, LINQ, Microsoft Office, MongoDB, MySQL, Node.js, PostgreSQL, SQL, SQL Server, TypeORM, TypeScript, Visual Basic .NET, Web API. He also got his skills in working with different integration and some known versioning tools. He is always ready to learn new things and he always tries his best on tasks that are assigned to him and gives the best possible outputs.

Recent Posts

Testing hk

Testing

2 years ago

Create and Used PIPE in angular

In this article, we have to show Create and Used PIPE in angular

2 years ago

Operation

Testing

2 years ago

Create and Used PIPE in angular

In this article, we have to show Create and Used PIPE in angular

2 years ago

Create and Used PIPE in angular

In this article, we have to show Create and Used PIPE in angular

2 years ago

TETS NEW

test

3 years ago