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

前端 未结 7 1693
不知归路
不知归路 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:40

    In Reactive forms. Binding can be done in the component file and usage of ngValue. For more details please go through the following link

    https://angular.io/api/forms/SelectControlValueAccessor

    import {Component} from '@angular/core';
    import {FormControl, FormGroup} from '@angular/forms';
    
    @Component({
      selector: 'example-app',
      template: `
        

    Form value: {{ form.value | json }}

    `, }) export class ReactiveSelectComp { states = [ {name: 'Arizona', abbrev: 'AZ'}, {name: 'California', abbrev: 'CA'}, {name: 'Colorado', abbrev: 'CO'}, {name: 'New York', abbrev: 'NY'}, {name: 'Pennsylvania', abbrev: 'PA'}, ]; form = new FormGroup({ state: new FormControl(this.states[3]), }); }

提交回复
热议问题