我试图在Angularmaterial 输入字段中显示图标,但不幸的是,该图标未显示.我使用了以下代码:

https://stackblitz.com/edit/stackblitz-starters-ngdhyg?file=src%2Fmain.ts

尽管我添加了带有matSuffix属性的mat-icon组件,但该图标仍不会显示.有人能帮助我并告诉我我做错了什么或者我是否错过了什么吗?

提前感谢您的帮助!

<form (ngSubmit)="onSubmit()" class="contact-form">
      <mat-form-field>
        <mat-label>Email</mat-label>
        <input matInput type="email" formControlName="email" placeholder="Ex. pat@example.com">
        <mat-icon color="primary" matSuffix>email</mat-icon>
      </mat-form-field>
  
       <button mat-raised-button color="primary" type="submit"> Save
       </button>
</form>

推荐答案

请找到下面的例子,其中它运行良好,我猜我们需要添加导入

还要确保您已在index.html中添加了字体的导入

<link rel="preconnect" href="https://fonts.gstatic.com">
  <link href="https://fonts.googleapis.com/css2?family=Roboto:wght@300;400;500&display=swap" rel="stylesheet">
  <link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">

分量ts

import { Component } from '@angular/core';
import { MatIconModule } from '@angular/material/icon';
import { MatInputModule } from '@angular/material/input';
import { MatButtonModule } from '@angular/material/button';
import { MatFormFieldModule } from '@angular/material/form-field';

/** @title Simple form field */
@Component({
  selector: 'form-field-overview-example',
  templateUrl: 'form-field-overview-example.html',
  styleUrl: 'form-field-overview-example.css',
  standalone: true,
  imports: [MatIconModule, MatFormFieldModule, MatButtonModule, MatInputModule],
})
export class FormFieldOverviewExample {
  onSubmit() {}
}

组件html

<form (ngSubmit)="onSubmit()" class="contact-form">
  <mat-form-field>
    <mat-label>Email</mat-label>
    <input
      matInput
      type="email"
      formControlName="email"
      placeholder="Ex. pat@example.com"
    />
    <mat-icon color="primary" matSuffix>email</mat-icon>
  </mat-form-field>

  <button mat-raised-button color="primary" type="submit">Save</button>
</form>

Stackblitz Demo

Angular相关问答推荐

NG构建后多余的国旗

Primeng 12Angular 12自定义库:错误符号字段集声明于...在编译保存后未从Primeng/fieldset中导出

未在ng bootstrap 模式中设置表单输入

带输入的单位测试Angular 分量.需要

如何防止打印后的空格后的HTML元素的Angular ?

如何在Angular功能路由保护中取消订阅RxJs主题

有没有办法订阅部分 BehaviorSubject 值的变化?

Angular 获取视频持续时间并存储在外部变量中

Angular:为什么在我的自定义 ErrorHandler 中订阅主题不触发?

有条件地取消所有内部可观察对象或切换到仅发出一个值的新对象

Angular模板错误:Property does not exist on type component

如何使用ngrx和effects 订阅成功回调

使用 rxjs 处理刷新令牌

Angular 2在单击子项时防止单击父项

使用 Jasmine 和 Karma 对 Angular 进行单元测试,错误:无法绑定到xxx,因为它不是xxxxxx的已知属性.

NgrxStore 和 Angular - 大量使用异步管道或在构造函数中只订阅一次

为什么Angular 12 'ng serve' 构建应用程序的速度很慢?

Angular @NGRX中这三个点的含义是什么

unexpected token < 错误

如何在项目中导入 Angular material?