Angular Material教程

Material7 自动完成

是一个 Angular 指令,用作特殊的输入控件,带有内置下拉列表以显示与自定义查询的所有可能匹配项。一旦用户在输入区域中键入,此控件就充当实时建议框。 可用于提供来自本地或远程数据源的搜索结果。
在本章中,我们将展示使用 Angular Material 绘制自动完成控件所需的配置。

创建 Angular 应用程序

按照以下步骤更新我们在 Angular 6-Project Setup 章节中创建的 Angular 应用程序-
步骤 描述
1 创建一个名为 materialApp 的项目,如 Angular 6-项目设置 章节所述。
2 修改app.module.tsapp.component.tsapp.component.cssapp.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
         };
      });
   }
}

结果

验证结果。
自动完成

详情

首先,我们创建了一个输入框,并使用 [matAutocomplete] 属性绑定了一个名为 auto 的自动完成功能。 然后,我们使用 mat-autocomplete 标签创建了一个名为 auto 的自动完成。 接下来,使用 *ng For 循环,创建选项。
昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4