Angular Material 7 - 自动完成

< mat-autocomplete> ,Angular Directive,用作特殊输入控件,内置下拉列表以显示与自定义查询的所有可能匹配项.一旦用户键入输入区域,该控件就充当实时建议框. < mat-autocomplete> 可用于提供本地或远程数据源的搜索结果.

在本章中,我们将展示绘制所需的配置使用Angular Material的自动完成控件.

创建Angular应用程序

按照以下步骤更新我们在 Angular 6中创建的Angular应用程序 - 项目设置章节&减号;

Step描述
1创建一个名为 materialApp 的项目,如 Angular 6  -  Project Setup 一章中所述.
2修改 app.module .ts app.component.ts app.component.css app.component.html ,如下所述.保持其余文件不变.
3编译并运行应用程序以验证实现的逻辑的结果.

以下是修改后的模块描述符的内容 app.module.ts .

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppComponent } from './app.component';
import {BrowserAnimationsModule} from '@angular/platform-browser/animations';
import {MatAutocompleteModule,MatInputModule} from '@angular/material';
import {FormsModule, ReactiveFormsModule} from '@angular/forms';
@NgModule({
   declarations: [
      AppComponent
   ],
   imports: [
      BrowserModule,
      BrowserAnimationsModule,
      MatAutocompleteModule,
      MatInputModule,
      FormsModule,
      ReactiveFormsModule
   ],
   providers: [],
   bootstrap: [AppComponent]
})
export class AppModule { }

以下是修改后的HTML主机文件的内容 app.component.html .

<form class = "tp-form">
   <mat-form-field class = "tp-full-width">
      <input type = "text" 
         placeholder = "US State" 
         aria-label = "Number" 
         matInput 
         [formControl] = "myControl" 
         [matAutocomplete] = "auto">
      <mat-autocomplete #auto = "matAutocomplete">
         <mat-option *ngFor = "let state of states" [value] = "state.value">
            {{state.display}}
         </mat-option>
      </mat-autocomplete>
   </mat-form-field>
</form>

以下是修改过的CSS文件的内容 app.component.css .

.tp-form {
   min-width: 150px;
   max-width: 500px;
   width: 100%;
}
.tp-full-width {
   width: 100%;
}

以下是修改过的ts文件的内容 app.component.ts .

import { Component } from '@angular/core';
import { FormControl } from "@angular/forms";
@Component({
   selector: 'app-root',
   templateUrl: './app.component.html',
   styleUrls: ['./app.component.css']
})
export class AppComponent {
   title = 'materialApp';
   myControl = new FormControl();
   states;
   constructor(){
      this.loadStates();
   }
   //build list of states as map of key-value pairs
   loadStates() {
      var allStates = 'Alabama, Alaska, Arizona, Arkansas, California, Colorado, Connecticut, Delaware,\
         Florida, Georgia, Hawaii, Idaho, Illinois, Indiana, Iowa, Kansas, Kentucky, Louisiana,\
         Maine, Maryland, Massachusetts, Michigan, Minnesota, Mississippi, Missouri, Montana,\
         Nebraska, Nevada, New Hampshire, New Jersey, New Mexico, New York, North Carolina,\
         North Dakota, Ohio, Oklahoma, Oregon, Pennsylvania, Rhode Island, South Carolina,\
         South Dakota, Tennessee, Texas, Utah, Vermont, Virginia, Washington, West Virginia,\
         Wisconsin, Wyoming';
      this.states =  allStates.split(/, +/g).map( function (state) {
         return {
            value: state.toUpperCase(),
            display: state
         };
      });
   }
}

结果

验证结果.

Autocomplete

详细信息

  • 首先,我们创建了一个输入框,并使用[matAutocomplete]属性绑定了一个名为 auto 的自动填充.

  • 然后,我们使用mat-autocomplete标签创建了名为 auto 的自动填充功能.

  • 下一步,使用* ng For循环,创建选项.