开发学院

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

教程正文

Angular Material 7 可展开面板

  <mat-expansion-panel>用于创建可展开的详细v/s摘要视图。

  <mat-expansion-panel-header>:代表标题部分。包含面板摘要,并作为展开或折叠面板的控件。

  <mat-panel-title>:代表面板标题。

  <mat-panel-description>:代表面板摘要。

  <mat-action-row>:表示底部的操作面板。

  在本章中,我们将展示使用Angular Material绘制可展开面板控件所需的配置。

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

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

<mat-expansion-panel>
   <mat-expansion-panel-header>
      <mat-panel-title>
         Personal data
      </mat-panel-title>
      <mat-panel-description>
         Type name and age
      </mat-panel-description>
   </mat-expansion-panel-header>
   <mat-form-field>
      <input matInput placeholder="Name">
   </mat-form-field>
   <mat-form-field>
      <input matInput placeholder="Age">
   </mat-form-field>
</mat-expansion-panel>

结果

angular_material7_expansion_panel.jpg

细节

  首先,我们使用mat-expansion-panel创建了展开面板。

  然后,我们添加了标题、副标题和内容。