在Angular Material中mat-select介绍
介绍:
Angular Material是一个UI组件库,由Angular团队开发,用于构建桌面和移动Web应用程序的设计组件。为了安装它,我们需要在我们的项目中安装Angular,一旦你有了它,你可以输入下面的命令并可以下载它。<mat-select>
标签是用来在下拉菜单中显示选项。
安装:
ng add @angular/material
步骤:
- 首先,使用上述命令安装Angular Material。
- 完成安装后,在app.module.ts文件中从’@angular/material/select’导入’MatSelectModule’。
- 然后使用
<mat-select>
标签将所有项目分组在这个组标签内。 - 在
<mat-select>
标签里面,我们需要为每一个项目使用<mat-option>
标签。 - 我们还可以使用disabled属性禁用选项。
- 一旦完成了上述步骤,就可以服务或开始项目。
代码实现:
app.module.ts:
import { CommonModule } from '@angular/common';
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { MatSelectModule,MatFormFieldModule }
from '@angular/material';
import { AppComponent } from './example.component';
@NgModule({
declarations: [AppComponent],
exports: [AppComponent],
imports: [
CommonModule,
FormsModule,
MatSelectModule,
MatFormFieldModule
],
})
export class AppModule {}
app.component.html:
<mat-form-field appearance="fill">
<mat-label>Choose a technology</mat-label>
<mat-select>
<mat-option value="html"> HTML
<mat-option value="css" disabled>CSS (disabled)
<mat-option value="js">JAVASCRIPT
</mat-form-field>
输出: