开发学院

您的位置:首页>教程>正文

教程正文

Angular Material 7 Chips

  <mat-chip-list>用于作为chip的值列表.

  在本章中,我们将展示使用Angular Material绘制chip控制所需的配置。

创建 Angular 应用

  按照以下步骤创建应用程序

  1.创建一个名为materialApp的项目。

  2.修改app.module.ts, app.component.ts, app.component.css and app.component.htmls。保持其余文件不变。

  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 {MatChipsModule} from '@angular/material'
import {FormsModule, ReactiveFormsModule} from '@angular/forms';
@NgModule({
   declarations: [
      AppComponent
   ],
   imports: [
      BrowserModule,
      BrowserAnimationsModule,
      MatChipsModule,
      FormsModule,
      ReactiveFormsModule
   ],
   providers: [],
   bootstrap: [AppComponent]
})
export class AppModule { }

  下面是修改后的app.component.html文件内容。

<mat-chip-list>
   <mat-chip>One</mat-chip>
   <mat-chip>Two</mat-chip>
   <mat-chip color = "primary" selected>Tree</mat-chip>
   <mat-chip color = "accent" selected>Four</mat-chip>
</mat-chip-list>

结果

angular_material7_chips.jpg

细节

  首先,我们使用mat-chip-list创建了chip列表。

  然后,我们使用mat-chip将chip添加到每个chip列表中。