首页 > 解决方案 > 如何为 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]了属性,但它似乎不起作用。

标签: angularintl-tel-input

解决方案


在 ngAfterViewInit 中,将带有国家代码的电话值修补到表单控件并检测更改。

ngAfterViewInit(){ this.phoneForm.controls.phone.setValue('+919898989898'); this.cd.detectChanges(); }


推荐阅读