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

前端 未结 7 1640
不知归路
不知归路 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) 
    
    0 讨论(0)
  • 2020-12-01 01:33

    You can use the patch function for setting defaults with some of the values in your form group.

    component.html

    <form [formGroup]="countryForm">
        <select id="country" formControlName="country">
            <option *ngFor="let c of countries" [ngValue]="c">{{ c }}</option>
        </select>
    </form>
    

    component.ts

    import { FormControl, FormGroup, Validators } from '@angular/forms';
    
    export class Component implements OnInit{
    
        countries: string[] = ['USA', 'UK', 'Canada'];
        default: string = 'UK';
    
        countryForm: FormGroup;
    
        constructor() {
    
            this.countryForm.controls['country'].setValue(this.default, {onlySelf: true});
        }
    }
    
    ngOnInit() {
        this.countryForm = new FormGroup({
          'country': new FormControl(null)
        }); 
    
        this.countryForm.patchValue({
          'country': default
        });
    
      }
    
    0 讨论(0)
  • 2020-12-01 01:36

    As option, if you need just default text in dropdown without default value, try add <option disabled value="null">default text here</option> like this:

    <select id="country" formControlName="country">
     <option disabled value="null">default text here</option>
     <option *ngFor="let c of countries" [value]="c" >{{ c }}</option>
    </select>
    

    In Chrome and Firefox works fine.

    0 讨论(0)
  • 2020-12-01 01:39

    You have to create a new property (ex:selectedCountry) and should use it in [(ngModel)] and further in component file assign default value to it.

    In your_component_file.ts

    this.selectedCountry = default;
    

    In your_component_template.html

    <select id="country" formControlName="country" [(ngModel)]="selectedCountry">
     <option *ngFor="let c of countries" [value]="c" >{{ c }}</option>
    </select> 
    

    Plunker link

    0 讨论(0)
  • 2020-12-01 01:39

    I was struggling and Found this Easy and Effective way from IntelliJ IDEA suggestion

    <select id="country" formControlName="country"  >
      <option [defaultSelected]=true [value]="default" >{{default}}</option>
      <option *ngFor="let c of countries" [value]="c" >{{ c }}</option>
    </select> 
    

    And On your ts file assign the values

     countries = ['USA', 'UK', 'Canada'];
     default = 'UK'
    

    Just make sure your formControlName accepts string, because you already assigned it as a string.

    0 讨论(0)
  • 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 [formGroup]="form">
          <select formControlName="state">
            <option *ngFor="let state of states" [ngValue]="state">
              {{ state.abbrev }}
            </option>
          </select>
        </form>
    
           <p>Form value: {{ form.value | json }}</p> 
           <!-- {state: {name: 'New York', abbrev: 'NY'} } -->
        `,
    })
    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]),
      });
    }
    
    0 讨论(0)
提交回复
热议问题