垫形场角材料内有多个输入

时间:2019-03-25 19:21:14

标签: angular-material

我想要实现的目标可以在下图中看到。

enter image description here

或stackblitz

https://angular6-material-components-demo-etqohr.stackblitz.io/

我正在使用角度材料作为UI框架

问题是,当我单击任意位置(日,月或年)时,焦点将自动置于第一个输入上。我无法在文档中找到如何禁用此行为。

另一个问题是验证,整个字段的验证只会考虑第一个字段。感觉这不受角材料的支持。有关如何解决此问题的任何想法?

模板标记:

<mat-form-field class="full-width">
  <mat-label>{{label}}</mat-label>
  <div class="d-flex justify-content-between">
    <div>
      <input matInput name="day" #day="ngModel" [(ngModel)]="dateOfBirthDay" type="tel" inputmode="numeric"
        autocomplete="nope" placeholder="24" required maxlength="2" (keypress)="validate($event)">
    </div>
    <div>
      <input matInput name="month" [(ngModel)]="dateOfBirthMonth" type="tel" inputmode="numeric" autocomplete="nope"
        placeholder="12" required maxlength="2" (keypress)="validate($event)">
    </div>
    <div>
      <input matInput name="year" [(ngModel)]="dateOfBirthYear" type="tel" inputmode="numeric" autocomplete="nope"
        [placeholder]="placeholderYear" required maxlength="4" (keypress)="validate($event)">
    </div>
  </div>
  <mat-error>
    {{_errorMessage}}
  </mat-error>
</mat-form-field>

1 个答案:

答案 0 :(得分:1)

为了更改角度材料组件的默认行为,最佳实践是对其进行扩展并创建自己的自定义类。

对于 mat-form-field 组件,Angular Material网站上有一个指南,它与您尝试的工作非常接近。我建议您检查一下,这里是链接;

https://material.angular.io/guide/creating-a-custom-form-field-control

链接中的示例还将输入分为3个部分,您可以集中其中的任何一个。

相关问题