tanansatpal / ngx-mat-intl-tel-input

MIT License
62 stars 108 forks source link

Unable to change preferred country #154

Open egilsd opened 2 years ago

egilsd commented 2 years ago

Im trying to set the preferred country depending on what i get back from the ip address. Im checking the users IP in the afterViewInit to get the country the user is in. Im trying to set that country as the preferred country for the ngx-mat-intl-tel-input component instead of hardcoding it in. Any suggestions as to how to make that possible?

I tried making an observable and async it, but that doesnt seem to work here unfortunately.

pavlikxor commented 1 year ago

Unfortunately current implementation doesn't accept any on-changes handler for an input property preferredCountries. I guess in such case the only way is to fire ChangeDetectorRef once you have the new set of countries ready.

Zetanova commented 1 year ago

@pavlikxor ChnageDetectorRef does not work

To set preferredCountriesInDropDown and/or selectedCountry is working

export class MuchbetterComponent {
      @ViewChild('phone') phoneElement!:NgxMatIntlTelInputComponent;

       //.... 

      setCountry(country:string) {
           const iso2 = country.toLowerCase();
          const foundCountry = this.phoneElement.allCountries.find(n => n.iso2 == iso2);
          if(foundCountry)
          {
            this.phoneElement.preferredCountriesInDropDown = [foundCountry];
            this.phoneElement.selectedCountry = foundCountry;
          }
      }

}