繁体   English   中英

更改角度材质 datePicker 图标

[英]change angular material datePicker icon

我想知道是否可以使用角度材料的 datePicker 更改显示的图标。

这是来自角度材料文档的代码。

<md-input-container>
  <input mdInput [mdDatepicker]="picker" placeholder="Choose a date">
  <button mdSuffix [mdDatepickerToggle]="picker"></button>
</md-input-container>
<md-datepicker #picker></md-datepicker>

有没有办法做到这一点?

您可以在 mat-datepicker-toggle 中添加 mat-icon 自定义

<input matInput [matDatepicker]="dp" placeholder="Select minimum date" disabled>
<mat-datepicker-toggle matSuffix [for]="dp">
  <mat-icon matDatepickerToggleIcon>arrow_drop_down</mat-icon>
</mat-datepicker-toggle>
<mat-datepicker #dp disabled="false"></mat-datepicker>

您可以通过覆盖mat-datepicker-toggle类的background属性来做到这一点。 asset文件夹中添加所需图标的路径。

这是在src > asset > img alert.png图标替换calender图标的示例:

>>> .mat-datepicker-toggle {
    background: url("../../assets/img/alert-circle-24.png") no-repeat !important;
}

html:

<md-input-container align="end">
   <input mdInput [mdDatepicker]="datepicker"
                  [(ngModel)]="date">
   <button mdSuffix [mdDatepickerToggle]="datepicker"></button>
</md-input-container>

在此处输入图像描述

您可以在其中添加自己的图像

<mat-datepicker-toggle matSuffix (click)="openCalendar()">
    <mat-icon matDatepickerToggleIcon>
        <img src={{imageSource}}>
    </mat-icon>
</mat-datepicker-toggle>

您可以将 svg 用于自定义图标,如下所示:

<mat-form-field style="width: 100%; margin-top: 16px;">
    <mat-label style="font-size: 16px;">Date</mat-label>
    <input matInput [matDatepicker]="protocolIssueDate" formControlName="protocolIssueDate">
    <mat-datepicker-toggle matSuffix [for]="protocolIssueDate">
        <mat-icon matDatepickerToggleIcon>
            <svg width="24" height="24" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
                <path d="M7 4L9 4V6L15 6V4L17 4V6L20 6V14H18V8L6 8L6 18H14V20H4L4 6H7V4Z" fill="black" fill-opacity="0.38"/>
                <path d="M10 12V10H8V12H10Z" fill="black" fill-opacity="0.38"/>
                <path d="M14 10L12 10V12L14 12V10Z" fill="black" fill-opacity="0.38"/>
                <path d="M10 16V14L8 14V16L10 16Z" fill="black" fill-opacity="0.38"/>
            </svg>
        </mat-icon>
    </mat-datepicker-toggle>
    <mat-datepicker #protocolIssueDate startView="month" [startAt]="startProtocolIssueDate"></mat-datepicker>
</mat-form-field>

检查这个https://stackblitz.com/edit/angular-ivy-xc5wkd?file=src%2Fapp%2Fapp.component.ts

在 Material 图标字体之外的自定义图标,这种方法对于开发人员来说将更加可重用和清晰。

<mat-form-field class="custom-mat-form-field" appearance="fill">
<mat-label>Choose a date 1</mat-label>
<input matInput [matDatepicker]="picker1" />
<mat-hint>MM/DD/YYYY</mat-hint>
<mat-datepicker-toggle matSuffix [for]="picker1">

  <!-- this icon is outside of Material icons font -->
  <mat-icon svgIcon="date_check_in" matDatepickerToggleIcon></mat-icon>
  <!-- you can check how do this in the link above -->
  
</mat-datepicker-toggle>
<mat-datepicker #picker1></mat-datepicker>

结果: 在此处输入图像描述

角 13

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM