【问题标题】:How to set default country flag when entering mobile number输入手机号码时如何设置默认国家标志
【发布时间】:2020-02-05 12:52:39
【问题描述】:

我正在尝试在我的选择框中添加默认国家标志(印度)。 我正在使用 Angular 4 国际电话前缀输入框。但我不知道如何设置默认值。他们将 github 上的文档共享为

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

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

我试过了,但它不起作用。

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

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

【问题讨论】:

  • 请添加您正在使用的软件包
  • 我已经编辑了问题。

标签: angular


【解决方案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>

【讨论】:

    【解决方案2】:
    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.
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-11-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-23
      • 1970-01-01
      • 2016-10-12
      • 1970-01-01
      相关资源
      最近更新 更多