简体   繁体   English

angular素材中的范围输入字段

[英]Range input field in angular material

How can I implement a range input field using the angular material form field?如何使用 angular 材料表单字段实现范围输入字段? It should look somehow similar to the range date picker but I could not figure out a way to implement this.它看起来应该有点类似于范围日期选择器,但我想不出实现它的方法。

I want to implement eg a number input where the user can input a range.我想实现例如数字输入,用户可以在其中输入范围。 This should look similar to this example with the range date picker from angular material but for a standard number input field.这应该看起来类似于此示例,其中包含来自 angular 材料的范围日期选择器,但用于标准数字输入字段。

I had a similar issue so I followed what @dt170 had commented on the question.我遇到了类似的问题,所以我遵循了@dt170 对这个问题的评论。 This is what worked for me:这对我有用:

<mat-form-field>
  <mat-label>From</mat-label>
  <input matInput #from type="number" formControlName="from" [max]="to.value">
</mat-form-field>

<mat-form-field>
  <mat-label>To</mat-label>
  <input matInput #to type="number" formControlName="to" [min]="from.value">
</mat-form-field>

It is quite a simple solution (may be imperfect but it works).这是一个非常简单的解决方案(可能不完美但有效)。 The key part is that the [max] and [min] of the input fields - they are set to the values of each other so the range is valid (ie from < to).关键部分是输入字段的[max][min] - 它们被设置为彼此的值,因此范围是有效的(即从 < 到)。

*Both fields are invalid when the range is not valid. *当范围无效时,两个字段都无效。

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

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