gauravsoni119 / ng2-tel-input

MIT License
56 stars 57 forks source link

Get value with country code #30

Open akvaliya opened 5 years ago

akvaliya commented 5 years ago

I have input like below

<input type="tel" ng2TelInput (intlTelInputObject)="telInputObjectWork($event)" formControlName="WorkPhone" class="form-control" [ngClass]="{ 'is-invalid': submitted && f.WorkPhone.errors }" />

When in use intlTelInput("setNumber", "+917878747474"); it sets flag correctly.

But when i change value it's i am getting formatted value without country code (078 7874 7489) in WorkPhone.

i want "+917878747489" when i save in db.

How can i achieve this?

najibla commented 5 years ago

Personally, the way I do it is as follows In the template in the input I have this output (countryChange)="countryChange($event)" And in the component I have the function that sets a class variable to the country code

  countryChange(country: any) {
    this.countryCode = country.dialCode;
  }

You can initialize that countryCode in the class to the code of your initialCountry that you set in the template like countryCode = 000 Then when you want to send to db:

sendToDB() {
    phoneValueToSend = '+' + this.countryCode + this.yourPhoneInputModel;
}

There probably is a better way that involves the intlTelInputObject.. I couldnt figure it out yet.