Angular Material 7 - 卡片



<mat-card> 是一個 Angular 指令,用於建立具有 Material Design 樣式和動畫功能的卡片。它為常見的卡片部分提供了預設樣式。

  • <mat-card-title> - 代表標題部分。

  • <mat-card-subtitle> - 代表副標題部分。

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

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

  • <mat-card-footer> - 代表頁尾部分。

在本章中,我們將展示使用 Angular Material 繪製卡片控制元件所需的配置。

建立 Angular 應用

按照以下步驟更新我們在Angular 6 - 專案設定章節中建立的 Angular 應用:

步驟 描述
1 按照Angular 6 - 專案設定章節中的說明,建立一個名為materialApp的專案。
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 {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 { }

以下是修改後的 CSS 檔案app.component.css的內容。

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

以下是修改後的 HTML 主機檔案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://tutorialspoint.tw/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>

結果

驗證結果。

Card

詳情

  • 在這裡,我們使用 mat-card 建立了一個卡片。
廣告