3

I have a select dropdown with a default selected disabled value, and a list of districts that is loaded from backend. My issue is that the dropdown never show the selected value, always show blank.

<form [formGroup]="districtForm">
  District:
  <select formControlName="districtControl" (change)="filterBuildings($event.target.value)" [value]='' class="form-control">
    <option value="" disabled selected>Please select district</option>
    <option *ngFor="let district of districts" [ngValue]="district">
      {{district.districtName}}
    </option>
  </select>
  <div *ngIf="errorMessageDistrict" class="text-danger">District is required
  </div>
</form>

Here is my ts code:

import { Component } from '@angular/core';
import { NgbActiveModal } from '@ng-bootstrap/ng-bootstrap';
import { InfoService } from '../../services/info.service';
import { first } from 'rxjs/operators';
import { District } from '../../models/district';
import { FormGroup, FormControl, FormBuilder } from '@angular/forms';

@Component({
  selector: 'app-school-switch',
  templateUrl: './school-switch.component.html',
  styleUrls: ['./school-switch.component.scss']
})
export class SchoolSwitchComponent {
  districts: District[] = [];
  selectedDistrict: District;
  errorMessageDistrict;
  firstDistrict: any;
  districtForm = new FormGroup({
    districtControl: new FormControl()
  });

  constructor(
    public activeModal: NgbActiveModal,
    private infoService: InfoService,
    private fb: FormBuilder
  ) {}

  ngOnInit() {
    this.infoService
      .getDistricts()
      .pipe(first())
      .subscribe(districts => {
        this.districts = districts;
      });
  }

  filterBuildings(filterVal: any) {
      this.selectedDistrict = this.districtForm.value.districtControl;
      //some more code
  }
2
  • Add ts code also Commented Sep 17, 2018 at 14:50
  • @Indrakumara done Commented Sep 17, 2018 at 18:20

4 Answers 4

3

Generally I create the FormGroup in the ngOnInit and then define default value in it like this :

import { FormGroup, FormBuilder, Validators } from '@angular/forms';

constructor(private formBuilder: FormBuilder){
}

ngOnInit(){
      this.districtForm = this.formBuilder.group({
        districtControl: ['myDefaultValue', Validators.required]
      });
}
Sign up to request clarification or add additional context in comments.

1 Comment

Default Option in select with value="" and included attributes selected or disabled. The dropdown won't work. Instead Use the validators. required. This works like charm. districtControl: ['myDefaultValue', Validators.required]
2

Get rid of [value] and instead use [(ngModel)] with selectedDistrict for select in your Template which would be initialized to 'none' in the TypeScript class. Also add [ngValue] to the Placeholder Option. Something like this:

<form [formGroup]="districtForm">
  District:
  <select 
    formControlName="districtControl" 
    [(ngModel)]="selectedDistrict" 
    class="form-control">
    <option 
      [ngValue]="'none'" 
      disabled 
      selected>
      Please select district
    </option>
    <option 
      *ngFor="let district of districts" 
      [ngValue]="district">
      {{district.districtName}}
    </option>
  </select>
  <div 
    *ngIf="errorMessageDistrict" 
    class="text-danger">
    District is required
  </div>
</form>

Here's a Sample StackBlitz for your reference.

2 Comments

It doesn't work either, besides I'm getting this warning: "It looks like you're using ngModel on the same form field as formControlName. Support for using the ngModel input property and ngModelChange event with reactive form directives has been deprecated in Angular v6 and will be removed in Angular v7."
It worked for me..thank you...changing from value to [ngvalue] worked..
0

Do not assign any default value in Form Control else your Validators.required will be of no use, instead use one default option in select with value = "" without disabled as shown below:

<form [formGroup]="districtForm">
  District:
  <select formControlName="districtControl" class="form-control">
    <option value="">Select District</option>
    <option *ngFor="let district of districts" [value]="district">
      {{district.districtName}}
    </option>
  </select>
  <div *ngIf="errorMessageDistrict" class="text-danger">District is required
  </div>
</form>

Form Control in Component.ts:

import { FormGroup, FormBuilder, Validators } from '@angular/forms';

constructor(private formBuilder: FormBuilder){
}

districtForm: FormGroup;

ngOnInit(){
      this.createDistrictForm();
}

createDistrictForm(){
    this.districtForm = this.formBuilder.group({
        districtControl: ['', Validators.required]
      });
}

Comments

0

You could do this way

    <select
                         
           [ngModel]="currentTimeZone"
           (change)="onSelectingValue($event)"
           #timeZone="ngModel"
                       >
                     <option *ngFor="let timeZone of allTimeZones"> 
 {{timeZone}}</option>
    </select>

In the Component you could have

allTimeZones: string = ["PST", "EST", "CT"];
currentTimeZone = "PST"

When it does not work? If you have a value for currentTimeZone which is different from one of allTimeZones. Example currentTimeZone = "MT" will NOT WORK

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.