输入手机号码时如何设置默认国家/地区标志

时间:2018-04-25 06:21:43

标签: angular

我正在尝试在我的选择框中添加默认国家/地区标志(印度)。 我正在使用Angular 4国际电话前缀输入框。但我没有得到如何设置默认值。他们在github上分享了文档

@Input()语言环境 可以提供ISO 639-1语言代码来设置可用语言:es:西班牙语,en:英语

@Input()defaultCountry 可以提供ISO 639-1国家/地区代码来设置默认国家/地区。

我试过这个但是没有用。

<int-phone-prefix formControlName="myPhone" [locale]="'hi'" [defaultCountry]="in"> </int-phone-prefix>

我正在使用ng4-intl-phone软件包, 请帮我解决这个问题

2 个答案:

答案 0 :(得分:1)

尝试不带属性绑定的defaultCountry,因为in是你的字符串而不是动态内容所以试试这个

<int-phone-prefix formControlName="myPhone" [locale]="'hi'" defaultCountry="in"> </int-phone-prefix>

<int-phone-prefix formControlName="myPhone" [locale]="'hi'" [defaultCountry]="'in'"> </int-phone-prefix>

答案 1 :(得分:0)

HTML
``` 
                <mat-form-field class="pd-mat-form-field-class" hideRequiredMarker>
                <label class="pd-mat-custom-label">{{"CELL_1" | translate }}</label>
                <mat-label></mat-label>
                <input ng2TelInput #intlInput 
                [ng2TelInputOptions]="cell1TelInput" 
                (hasError)="hasError1($event)" 
                (ng2TelOutput)="getNumberCellNo1($event)" 
                (intlTelInputObject)="cell1telInputObject($event)" 
                (countryChange)="onCell1CountryChange($event)" 
                type="text" 
                maxlength=14  class="pd-mat-form-input-margin"  
                matInput  name="cell_no1"
                formControlName = "cellnumber1" required>
              `</mat-form-field>`
```

Javascript
` 
```

cell1TelInput = {
initialCountry: 'za', //default country
autoPlaceholder: 'polite',
nationalMode :true,
customPlaceholder: function(selectedCountryPlaceholder) {
  return 'Example : ' + selectedCountryPlaceholder;
}

  if (response.userCellNumbers[0] && response.userCellNumbers[0].cell_no) {
            this.cellnumber1 = response.userCellNumbers[0].cell_no;  
            this.cell_code1.setNumber(this.cellnumber1)     
          }

 getNumberCellNo1(e:any)
  {
    this.cell_code1.setNumber(e) 
    this.cellnumber1=e
  }

  public onCell1CountryChange(e: any) {
    this.cellnumber1DialCode =  e.dialCode;
    this.cell1TelInput.initialCountry = e.iso2
    this.cell_code1.setNumber("") 
  }

  public cell1telInputObject(obj) {
    this.cell_code1=obj
  }

  hasError1(event: any): void {
    if (!event && this.uploadForm.value.cellnumber1 ) {
    this.uploadForm.get('cellnumber1').setErrors(['invalid_cell_phone', true]);
    }
    }

  if (this.uploadForm.value.cellnumber1) {
      this.uploadForm.value.cellnumber1 = this.cellnumber1
      this.cellNumbers.push( {"cell_no": this.uploadForm.value.cellnumber1.replace(/\s/g, ""), "cell_type": 1});
    }`

```
    hasError() is used for validation
    The getNumber() will give you the country code and number as +919843133490.
    The setNumber() will set the country flag and place the number in input field.
    ng2TelInputOptions is used to initialize data

    Hope it helped. Any further queries can be asked.