简体   繁体   English

Ionic [Angular] - 反应形式:使用 setValue() selectionStart/End 后不会移动 cursor

[英]Ionic [Angular] - Reactive form: after using setValue() selectionStart/End doesn't move cursor

I want to make a responsive form with input handler that puts "."我想用放置“。”的输入处理程序制作一个响应式表单。 sign (via setValue()) at the end of the input field after keyboard input occurred.发生键盘输入后,在输入字段的末尾签名(通过 setValue())。 At the same time I want that after input occurred the cursor moves to the beginning of the input field and I use for that 'selectionStart/selectionEnd' properties of the input tag.同时我希望在输入发生后 cursor 移动到输入字段的开头,我使用输入标签的“selectionStart/selectionEnd”属性。

If I'm using plain html, everything works fine.如果我使用普通的 html,一切正常。 But with ionic input it doesn't work and the cursor moves to the end after the keyboard input occurred.但是使用离子输入它不起作用,并且 cursor 在键盘输入发生后移动到末尾。 As is a wrapper for another input, I tried both setting selectionStart/selectionEnd properties for initial ion-input and its child but without success.作为另一个输入的包装器,我尝试为初始离子输入及其子输入设置 selectionStart/selectionEnd 属性,但没有成功。

How to cause the cursor to move to the beginning of the input field in Ionic input after changing form control value in ts file?在ts文件中更改表单控件值后,如何使cursor移动到Ionic输入中输入字段的开头?

home.page.ts:主页.ts:

export class HomePage {
  form = new FormGroup({
    amount: new FormControl(''),
  });
  constructor() {}

  amountHandler(event: any) {
    this.form.get('amount').setValue(event.target.value + '!');
    event.target.firstChild.selectionStart = 0;
    event.target.firstChild.selectionEnd = 0;
    //event.target.selectionStart = 0;
    //event.target.selectionEnd = 0;
  }
}

home.page.html:主页.html:

<form [formGroup]="form">
  <ion-item>
    <ion-label>Amount: </ion-label>
    <ion-input
      (ionInput)="amountHandler($event)"
      formControlName="amount"
    ></ion-input>    
  </ion-item>
</form>

setValue() is processed asynchronically, ie it happens after your selectionStart . setValue()是异步处理的,即它发生在您的selectionStart之后。 Try adding https://angular.io/api/core/ApplicationRef#tick after the setValue() .尝试在setValue()之后添加https://angular.io/api/core/ApplicationRef#tick

tick() Invoke this method to explicitly process change detection and its side-effects. tick() 调用此方法以显式处理更改检测及其副作用。

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

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