开发学院

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

教程正文

Angular Material 7 Card布局

  <mat-card>用于创建具有Angular Material样式和动画功能的Card布局。

  •   <mat-card-title>-代表标题部分。

  •   <mat-card-subtitle>-表示副标题部分。

  •   <mat-card-content>-代表内容部分。

  •   <mat-card-actions>-代表动作部分。

  •   <mat-card-footer>-代表页脚部分。

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

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

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

.tp-card {
   max-width: 400px;
}
.tp-header-image {
   background-image: url('https://www.tutorialspoint.com/materialize/src/html5-mini-logo.jpg');
   background-size: cover;
}

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

<mat-card class = "tp-card">
   <mat-card-header>
      <div mat-card-avatar class = "tp-header-image"></div>
      <mat-card-title>HTML5</mat-card-title>
      <mat-card-subtitle>HTML Basics</mat-card-subtitle>
   </mat-card-header>
   <img mat-card-image src = "https://www.tutorialspoint.com/materialize/src/html5-mini-logo.jpg" alt = "Learn HTML5">
   <mat-card-content>
      <p>
         HTML5 is the next major revision of the HTML standard superseding
         HTML 4.01, XHTML 1.0, and XHTML 1.1. HTML5 is a standard for
         structuring and presenting content on the World Wide Web.
      </p>
   </mat-card-content>
   <mat-card-actions>
      <button mat-button>LIKE</button>
      <button mat-button>SHARE</button>
   </mat-card-actions>
</mat-card>

结果

angular_material7_card.jpg

细节

  这里,我们用mat-card制作了一个卡片布局。