I use ng2-tel-input with reactive form method (Angular).
Issue:
When i open flag dropdown and choose a value, it is working fine.
Value is displayed into input correctly but country flag do not change and the default flag appear.
When I change value manually in input then flag changes automatically, otherwise default country flag displaying.
Example:
Value format: +917878747474
Selected country: India
Default country: us
when i press on set value. phone no set on input but flag remain same
html
<form [formGroup]="form">
<small class="phone-text">{{ placeHolder }}</small>
<md-input-container class="full-width pt-0">
<input type="text"
mdInput
formControlName="phone"
ng2TelInput
[ng2TelInputOptions]="{initialCountry: initialCountry}"
(hasError)="hasError($event)"
(ng2TelOutput)="hasOutPut($event)"
#phoneInput
maxlength="45"/>
</md-input-container>
<button type="button" (click)="setValue()">Set Value</button>
</form>
Component
import {Attribute, Component, forwardRef, Input} from '@angular/core';
import {FormBuilder, FormGroup} from '@angular/forms';
@Component({
selector: 'app-phone',
templateUrl: './phone.component.html',
styleUrls: ['./phone.component.scss']
})
export class PhoneComponent implements ControlValueAccessor, Validator {
placeHolder = 'Default Value';
initialCountry = 'us';
form: FormGroup;
constructor(private _fb: FormBuilder) {
this._initForm();
}
setValue(): void {
const phone = '+917878747416'
this.form.setValue({phone: phone});
}
private _initForm(): void {
this.form = this._fb.group({
'phone': ['']
});
}
hasError(event: any): void {
if (!event && this.form.value.phone !== '') {
this.form.get('phone').setErrors(['invalid_cell_phone', true]);
}
}
hasOutPut(event: any): void {
this.form.patchValue({phone: event});
}
}
Any help would be appreciated.