Angular 4 - Select default value in dropdown [Reactive Forms]

前端 未结 7 1688
不知归路
不知归路 2020-12-01 00:52

In Angular 4, I have the following configuration defined in a json config file.

 countries: [\'USA\', \'UK\', \'Canada\'];
 default: \'UK\'

7条回答
  •  臣服心动
    2020-12-01 01:19

    In your component -

    Make sure to initialize the formControl name country with a value.

    For instance: Assuming that your form group name is myForm and _fb is FormBuilder instance then,

    ....
    this.myForm = this._fb.group({
      country:[this.default]
    })
    

    and also try replacing [value] with [ngValue].

    EDIT 1: If you are unable to initialize the value when declaring then set the value when you have the value like this.

    this.myForm.controls.country.controls.setValue(this.country) 
    

提交回复
热议问题