首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >切换到mat-input时不一致的选择行为

切换到mat-input时不一致的选择行为
EN

Stack Overflow用户
提问于 2020-04-19 10:52:23
回答 1查看 19关注 0票数 0

我正在使用一组材料垫输入控件,当用户进入控件时,我不太理解选择逻辑。在某些情况下,我会选择字段的整个值,在其他情况下,光标位于现有输入的左侧。

请参见下面的示例:

EX1光标位于预先存在的值的最后一个字符之后:

Ex 2-选择整数值。

如何控制此行为?如果是输入字段中的前置值,我希望选择整个内容,这样,如果用户想要更改它,他们可以开始键入。

下面是input元素的html:

代码语言:javascript
复制
<mat-form-field class="input-cell" [ngClass]='inputClass'
   *ngIf="rowFocus && (inputType==InputTypeEnum.textInput  || inputType==InputTypeEnum.numberInput || inputType==InputTypeEnum.percentInput)">

   <mat-label>{{fieldName}}</mat-label>

   <input matInput placeHolder="placeholder" [(ngModel)]="value" (focus)="gotFocus()" (click)="inputClick($event)"
   [disabled]="readOnly"
      (blur)="lostFocus($event)" [errorStateMatcher]="matcher" #cellInput
      (keydown)="keyDown($event)"
       >
   <mat-error>{{errorText}}</mat-error>
</mat-form-field>
EN

回答 1

Stack Overflow用户

发布于 2020-04-19 23:49:18

我可以通过延迟调用焦点事件中的HTMLElement上的select来解决这个问题,如下所示:

添加到组件中:

代码语言:javascript
复制
 @ViewChild('cellInput') inputElement: ElementRef; //refers to the input element

 gotFocus() {
  setTimeout(()=>{ this.inputElement.nativeElement.select();},100)
 }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61298961

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档