【发布时间】:2019-12-19 00:30:57
【问题描述】:
我有授权组件,用户使用他们的电话号码作为登录名。还有问题是输入一个符号(不管是字母还是数字)input 失去焦点,用户需要再次单击它才能进一步输入。UPD: 忘了说有 3 个输入。一开始会有第一个输入,当它为空时。在输入第一个数字后,它将被第二个或第三个输入替换,具体取决于哪个国家的用户。对于某些国家,它将被替换为带有 +7 前缀的输入,对于另一个带有 +9 前缀的输入
html:
<input [(ngModel)]="phone"
*ngIf="isPhoneEmpty()"
class="phone-input m-b-50"
id="phone-input"
type="text"
name="phone"
minlength=18
placeholder="Phone number"
required>
<input [(ngModel)]="phone"
*ngIf="checkCountry() && !isPhoneEmpty()"
class="phone-input m-b-50"
id="phone-input"
type="text"
name="phone"
minlength=18
placeholder="Phone number"
prefix="+7"
mask=" (000) 000-00-00"
autofocus
required>
<input [(ngModel)]="phone"
*ngIf="!checkCountry() && !isPhoneEmpty()"
class="phone-input m-b-50"
id="phone-input"
type="text"
name="phone"
placeholder="PhoneNumber"
prefix="+996"
mask=" (000) 000-000"
autofocus
required>
组件:
import { HostListener} from '@angular/core';
phone: string;
checkCountry() {
return this.currentCountry.name === this.countryList[0].name;
}
isPhoneEmpty() {
return this.phone === '';
}
@HostListener('document:keypress', ['$event'])
handleKeyboardEvent(event: KeyboardEvent) {
const numbers = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'];
if (numbers.indexOf(event.key) !== -1) {
this._documentRef.getElementById('phone-input').focus();
}
}
【问题讨论】:
-
去掉
*ngIf还会出现这种情况吗? -
尝试将其更改为 type='tel'
-
@Reactgular 添加了更多信息
-
您是否考虑过使用响应式表单来更好地控制表单控件?这也将允许您使用每个输入的
valueChangesObservables。直接访问 DOM 是 Angular 的反模式。
标签: javascript html angular typescript