赞
踩
Angular Material 是 Angular 的一个官方库,用于构建遵循 Material Design 规范的用户界面。它提供了许多组件,如按钮、表单控件、布局工具、对话框、抽屉、滑块等,帮助开发者快速创建美观且响应式的应用。
首先,确保你已经安装了 Angular CLI 和 Angular Material。如果你还没有安装,可以使用以下命令:
npm install -g @angular/cli
ng new my-app
cd my-app
ng add @angular/material
这将创建一个新的 Angular 应用并添加 Angular Material 和 Angular CDK。
接下来,让我们创建一个简单的页面,包含一个导航栏、一个按钮和一个对话框:
src/app
目录下创建一个名为 app.component.html
的模板文件,添加以下内容:<!-- 导航栏 -->
<mat-toolbar color="primary">
<span>My App</span>
</mat-toolbar>
<!-- 主要内容区域 -->
<div class="content">
<button mat-raised-button (click)="openDialog()">Open Dialog</button>
</div>
app.component.ts
文件,引入所需的依赖并添加对话框逻辑:import { Component } from '@angular/core';
import { MatDialog } from '@angular/material/dialog';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.scss']
})
export class AppComponent {
constructor(private dialog: MatDialog) {}
openDialog() {
this.dialog.open(DialogComponent, { width: '250px' });
}
}
dialog.component.ts
和 dialog.component.html
,用于对话框的内容:// dialog.component.ts
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'app-dialog',
templateUrl: './dialog.component.html',
styleUrls: ['./dialog.component.scss']
})
export class DialogComponent implements OnInit {
constructor() { }
ngOnInit(): void {
}
}
<!-- dialog.component.html -->
<h2 mat-dialog-title>Dialog Title</h2>
<mat-dialog-content>
This is the content of the dialog.
</mat-dialog-content>
<mat-dialog-actions>
<button mat-button mat-dialog-close>CANCEL</button>
<button mat-button [mat-dialog-close]="tru
e">OK</button>
</mat-dialog-actions>
app.module.ts
中导入所需的 Angular Material 模块:import { BrowserModule } from '@angular/platform-browser'; import { NgModule } from '@angular/core'; import { BrowserAnimationsModule } from '@angular/platform-browser/animations'; import { MatButtonModule, MatToolbarModule, MatDialogModule } from '@angular/material'; import { AppComponent } from './app.component'; import { DialogComponent } from './dialog.component'; @NgModule({ declarations: [ AppComponent, DialogComponent ], imports: [ BrowserModule, BrowserAnimationsModule, MatButtonModule, MatToolbarModule, MatDialogModule ], providers: [], bootstrap: [AppComponent], entryComponents: [DialogComponent] // 添加DialogComponent作为入口组件 }) export class AppModule { }
现在,当你运行 ng serve
并访问应用时,你会看到一个带有导航栏和按钮的页面。点击按钮会打开一个对话框。这个例子展示了 Angular Material 如何轻松地集成到 Angular 应用中,创建出符合 Material Design 标准的组件。
Angular Material 还提供了许多其他组件,如表单控件、数据表、滑块、进度条、抽屉、侧边栏等。
<mat-form-field appearance="fill">
<mat-label>Email</mat-label>
<input matInput type="email" placeholder="example@example.com">
<mat-error *ngIf="emailInvalid">Please enter a valid email address</mat-error>
</mat-form-field>
<mat-form-field appearance="fill">
<mat-label>Choose a color</mat-label>
<mat-select [(value)]="selectedColor">
<mat-option value="red">Red</mat-option>
<mat-option value="green">Green</mat-option>
<mat-option value="blue">Blue</mat-option>
</mat-select>
</mat-form-field>
<table mat-table [dataSource]="dataSource"> <!-- Position Column --> <ng-container matColumnDef="position"> <th mat-header-cell *matHeaderCellDef> No. </th> <td mat-cell *matCellDef="let element"> {{element.position}} </td> </ng-container> <!-- Name Column --> <ng-container matColumnDef="name"> <th mat-header-cell *matHeaderCellDef> Name </th> <td mat-cell *matCellDef="let element"> {{element.name}} </td> </ng-container> <!-- Weight Column --> <ng-container matColumnDef="weight"> <th mat-header-cell *matHeaderCellDef> Weight </th> <td mat-cell *matCellDef="let element"> {{element.weight}} </td> </ng-container> <tr mat-header-row *matHeaderRowDef="displayedColumns"></tr> <tr mat-row *matRowDef="let row; columns: displayedColumns;"></tr> </table>
<mat-slide-toggle [(ngModel)]="isChecked">Toggle me</mat-slide-toggle>
<mat-progress-spinner mode="indeterminate" diameter="50"></mat-progress-spinner>
<mat-sidenav-container>
<mat-sidenav #drawer mode="over" opened="true">
Sidenav content
</mat-sidenav>
<mat-sidenav-content>
Main content
</mat-sidenav-content>
</mat-sidenav-container>
<button mat-raised-button>
<mat-icon>home</mat-icon>
Home
</button>
<mat-card>
<mat-card-header>
<mat-card-title>Card Title</mat-card-title>
<mat-card-subtitle>Subtitle</mat-card-subtitle>
</mat-card-header>
<img mat-card-image src="image-url.jpg" alt="Image description">
<mat-card-content>
<p>Card content goes here...</p>
</mat-card-content>
<mat-card-actions>
<button mat-button>ACTION</button>
</mat-card-actions>
</mat-card>
<mat-horizontal-stepper linear> <mat-step [stepControl]="firstFormGroup"> <form [formGroup]="firstFormGroup"> <ng-template matStepLabel>Fill out your name</ng-template> <mat-form-field> <input matInput placeholder="Last name, First name" formControlName="firstName" required> </mat-form-field> <div> <button mat-button matStepperNext>Next</button> </div> </form> </mat-step> <mat-step [stepControl]="secondFormGroup"> <form [formGroup]="secondFormGroup"> <ng-template matStepLabel>Fill out your address</ng-template> <mat-form-field> <textarea matInput placeholder="Address" formControlName="address" required></textarea> </mat-form-field> <div> <button mat-button matStepperPrevious>Back</button> <button mat-button matStepperNext>Next</button> </div> </form> </mat-step> <mat-step> <ng-template matStepLabel>Done</ng-template> You are now done. <div> <button mat-button matStepperPrevious>Back</button> <button mat-button (click)="stepper.reset()">Reset</button> </div> </mat-step> </mat-horizontal-stepper>
import { MatSnackBar } from '@angular/material/snack-bar';
// ...
constructor(private snackBar: MatSnackBar) {}
showSnackBar() {
this.snackBar.open('SnackBar message!', 'Close', {
duration: 2000, // 持续时间(毫秒)
});
}
<button mat-button (click)="showSnackBar()">Show SnackBar</button>
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。