Angular 5 Autocomplete Input-Text and Options - LearnHowToCode SarkariResult.com Interview Questions and Answers LearnHowToCodeOnline
Angular 5 Autocomplete

Angular 5 Autocomplete Input-Text and Options

In this tutorial, you’ll learn how to implement autocomplete functionality is a normal text input extended by a panel of suggested options i.e.





autocomplete.html –


<form class="class-form">
  <mat-form-field class="class-full-width">
    <input type="text" placeholder="Select a user" aria-label="Number" matInput[formControl]="myControl" [matAutocomplete]="auto">
    <mat-autocomplete #auto="matAutocomplete">
      <mat-option *ngFor="let user of users" [value]="user">
        {{ user }}
      </mat-option>
    </mat-autocomplete>
  </mat-form-field>
</form>

And autocomplete.ts –


import {Component} from '@angular/core';
import {FormControl} from '@angular/forms';

/** Simple autocomplete */
@Component({
selector: 'autocomplete',
templateUrl: 'autocomplete.html',
styleUrls: ['autocomplete.css']
})
export class Autocomplete {

myControl: FormControl = new FormControl();

users = [
'Sophia',
'Isabella',
'Emma',
'Olivia',
'Abigail',
'Elizabeth'
];
}

And NgModule with angular material file –
import {BrowserModulefrom '@angular/platform-browser';
import {platformBrowserDynamicfrom '@angular/platform-browser-dynamic';
import {BrowserAnimationsModulefrom '@angular/platform-browser/animations';

import {CdkTableModulefrom '@angular/cdk/table';
import {HttpClientModulefrom '@angular/common/http';
import {NgModulefrom '@angular/core';
import {FormsModuleReactiveFormsModulefrom '@angular/forms';
import {HttpModulefrom '@angular/http';

import {Autocompletefrom './app/autocomplete';

import {
  MatAutocompleteModule,
  MatButtonModule,
  MatButtonToggleModule,
  MatCardModule,
  MatCheckboxModule,
  MatChipsModule,
  MatDatepickerModule,
  MatDialogModule,
  MatDividerModule,
  MatExpansionModule,
  MatGridListModule,
  MatIconModule,
  MatInputModule,
  MatListModule,
  MatMenuModule,
  MatNativeDateModule,
  MatPaginatorModule,
  MatProgressBarModule,
  MatProgressSpinnerModule,
  MatRadioModule,
  MatRippleModule,
  MatSelectModule,
  MatSidenavModule,
  MatSliderModule,
  MatSlideToggleModule,
  MatSnackBarModule,
  MatSortModule,
  MatStepperModule,
  MatTableModule,
  MatTabsModule,
  MatToolbarModule,
  MatTooltipModule,
from '@angular/material';

@NgModule({
  exports: [
    CdkTableModule,
    MatAutocompleteModule,
    MatButtonModule,
    MatButtonToggleModule,
    MatCardModule,
    MatCheckboxModule,
    MatChipsModule,
    MatStepperModule,
    MatDatepickerModule,
    MatDialogModule,
    MatDividerModule,
    MatExpansionModule,
    MatGridListModule,
    MatIconModule,
    MatInputModule,
    MatListModule,
    MatMenuModule,
    MatNativeDateModule,
    MatPaginatorModule,
    MatProgressBarModule,
    MatProgressSpinnerModule,
    MatRadioModule,
    MatRippleModule,
    MatSelectModule,
    MatSidenavModule,
    MatSliderModule,
    MatSlideToggleModule,
    MatSnackBarModule,
    MatSortModule,
    MatTableModule,
    MatTabsModule,
    MatToolbarModule,
    MatTooltipModule,
  ]
})
export class DemoMaterialModule {}

@NgModule({
  imports: [
    BrowserModule,
    BrowserAnimationsModule,
    FormsModule,
    HttpModule,
    HttpClientModule,
    DemoMaterialModule,
    MatNativeDateModule,
    ReactiveFormsModule,
  ],
  entryComponents: [Autocomplete],
  declarations: [Autocomplete],
  bootstrap: [Autocomplete],
  providers: []
})
export class AppModule {}

platformBrowserDynamic().bootstrapModule(AppModule);


About Mariano

I'm Ethan Mariano a software engineer by profession and reader/writter by passion.I have good understanding and knowledge of AngularJS, Database, javascript, web development, digital marketing and exploring other technologies related to Software development.

0 comments:

Featured post

Political Full Forms List

Acronym Full Form MLA Member of Legislative Assembly RSS Really Simple Syndication, Rashtriya Swayamsevak Sangh UNESCO United Nations E...

Powered by Blogger.