简体   繁体   English

Angular 6 反应形式输入值到大写

[英]Angular 6 Reactive Form Input Value to UpperCase

I am using Reactive form in Angular 6. For input type text I want it to be uppercase.我在 Angular 6 中使用 Reactive 表单。对于输入类型文本,我希望它是大写的。 I tried the solution我尝试了解决方案

(input)="form.patchValue({name: $event.target.value.toUpperCase()})"

The solution works fine, but the only problem when I move cursor to middle and type a character, the cursor moves at the end.该解决方案工作正常,但是当我将光标移动到中间并键入一个字符时,唯一的问题是光标移动到最后。

Is there any other approach or any better solution?有没有其他方法或更好的解决方案?

why don't you just use CSS to do the job?为什么不直接使用 CSS 来完成这项工作?

 .uppercase{ text-transform: uppercase; }
 <input class="uppercase" type="text" placeholder="type here">

You can try this:你可以试试这个:

const yourControl = this.form.get('yourControlName');
yourControl.valueChanges.subscribe(() => {
  yourControl.patchValue(yourControl.value.toUpperCase(), {emitEvent: false});
});

I know this is reeeeally late, but...我知道这已经很晚了,但是...

You could hook on to the (change) event instead of the (input) event.您可以连接到 (change) 事件而不是 (input) 事件。 Your case changes won't execute until after the user leaves the field, but it will prevent the cursor from jumping.直到用户离开该字段后,您的案例更改才会执行,但它会阻止光标跳转。

You case changes will still execute if the user submits the form by pressing Enter while in the field.如果用户在字段中按 Enter 提交表单,您的案例更改仍将执行。

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

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