angular - 如何为 ngx-intl-tel-input 设置初始值
问题描述
我在我的 Angular 项目中使用 ngx-intl-tel-input 进行电话号码验证。
<form #f="ngForm" [formGroup]="phoneForm">
<ngx-intl-tel-input
[cssClass]="'custom'"
[preferredCountries]="['us', 'gb']"
[enablePlaceholder]="true"
[enableAutoCountrySelect]="true"
[value]="'+91 8888888888'"
name="phone"
formControlName="phone"></ngx-intl-tel-input>
我需要为来自服务器的字段设置值。
我使用[value]
了属性,但它似乎不起作用。
解决方案
在 ngAfterViewInit 中,将带有国家代码的电话值修补到表单控件并检测更改。
ngAfterViewInit(){
this.phoneForm.controls.phone.setValue('+919898989898');
this.cd.detectChanges();
}
推荐阅读
- python - Python 3.6 正则表达式产生意外结果(尽管使用字符串文字)
- angular - 无法在 Ionic 的真实 android 设备中获取 REST API 响应
- javascript - 如何正确使用.then
- fftw - FFT 输入数组编号?
- javascript - VS Code 调试 PHP 和 Javascript
- firebase - 无法部署云功能
- javascript - 如何在没有硬编码的情况下传递数组的索引来更新“react-addons-update”的方法?
- sql - SQL Cast 怪癖
- regex - 301用正则表达式重定向404页面
- javascript - 从数组/Javascript中删除具有空值的对象