简体   繁体   English

如何在角度反应形式控件中添加小数,其值从 1 和更大开始

[英]how to add decimal in angular reactive form control with value starting with 1 and greater

I want to add a decimal and then 0 after 1 like 1.0我想添加一个小数,然后在 1 之后添加 0,例如 1.0

<input type="number" formControlName="global_velocity_weight" />

this.form = this.fb.group({
   global_velocity_weight: new FormControl(1.0, { validators: [Validators.required] })
})

But it is not working and in the input, only 1 is shown.但它不起作用,在输入中,只显示 1。

you can achieve this using angular DecimalPipe inside your form template.您可以在表单模板中使用 angular DecimalPipe来实现这一点。

<form [formGroup]="myForm">
    <input 
        [value]="myForm.get('global_velocity_weight').value | number" // here you pipe the value
        formControlName="global_velocity_weight" 
    
        ...
    >
</form>

You should make some regular expression to accept numeric values:您应该制作一些正则表达式来接受数值:

 <input type="number" formControlName="global_velocity_weight" />
    
    this.form = this.fb.group({
       global_velocity_weight: new FormControl(1.0, { validators: 
    [Validators.required,Validators.pattern('^[0-9]+(.[0-9]{0,1})?$')] })
    })

I think your best shot would be to apply a mask to the input field.我认为您最好的办法是在输入字段中应用蒙版。 I know of a library ( https://github.com/JsDaddy/ngx-mask ) that allows you to do so and I believe it's supported from Angular 5 up.我知道一个库( https://github.com/JsDaddy/ngx-mask )允许你这样做,我相信它从 Angular 5 开始支持。

A mask is a specification of what the input should look like, like yyyy/mm/dd you see often for date inputs.掩码是输入应该是什么样子的规范,例如您经常看到的日期输入的yyyy/mm/dd You can also use this to specify how a number input field should look like and indicate the appearance of decimals.您还可以使用它来指定数字输入字段的外观并指示小数的外观。

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

相关问题 在以角4反应形式进行控件初始化后,如何添加自定义验证? - How to add custom validation after control initialization in angular 4 reactive form? 如何使用从回调函数获得的值以角度设置反应形式控件的值? - How to set value in a reactive form control in angular, using the value obtained from the call back function? Angular - 测试响应式表单控件启用值更改 - Angular - test reactive form control enable on value changes Angular 6反应形式-如何设置条件值? - Angular 6 Reactive Form - how to set conditional value? 如何为 angular 反应式表单中的表单值添加自定义唯一验证器? - How to add custom unique validator for form values in angular reactive form? 角反应形式克隆值 - Angular Reactive Form clone value Angular 反应式表单 - 禁用所需的表单控件使表单有效,即使没有给出值 - Angular reactive form - Disabling a required form control makes the form valid even if no value is given Angular Reactive Form 单元测试:HTML 值和控制值不同步 - Angular Reactive Form Unit Testing: HTML value and control value out of sync 如何有条件地在 Angular Reactive Form 中设置 [disabled] 属性值? - How to conditionally set a [disabled] attribute value in an Angular Reactive Form? 如何在Angular 6中设置反应形式的日期默认值? - How to set date default value for reactive form in Angular 6?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM