0

In Angular, I want to select the default value in the select boxes.

import { FormArray, FormBuilder, FormGroup, Validators } from '@angular/forms';
....
....
export class CastComponent implements OnInit {
public form: FormGroup;
public contactList: FormArray;
get contactFormGroup() {
  return this.form.get('contacts') as FormArray;
}
ngOnInit() {
  this.form = this.fb.group({
    crew_name: [null],
    crew_role: [null],
    contacts: this.fb.array([this.createContact()]),
  });
  this.contactList = this.form.get('contacts') as FormArray;
}
...
...
}

In the .html file:

<form [formGroup]="form" (submit)="submit()">
<p class="mb-3">&nbsp;</p>
<h3>Crew</h3>
<div class="row mb-4">
    <div class="col-sm-12 col-md-6 col-lg-6">
        <label class="required">Member Name</label>
        <input type="text" class="form-control" >
    </div>
    <div class="col-sm-12 col-md-6 col-lg-5">
        <label class="required">Role</label>
        <!-- <input type="text" class="form-control" value="Director"> -->
        <select class="form-control" type="text" >
          <option [ngValue]="">Select</option>
          <option [ngValue]="1">Director</option>
          <option [ngValue]="2">Music Composer</option>
          <option [ngValue]="3">Writer</option>
        </select>
    </div>
</div>

<span formArrayName="contacts">
  <div class="row mb-4" *ngFor="let contact of contactFormGroup.controls; let i = index;">
  <div [formGroupName]="i" class="col-sm-12 col-md-12 col-lg-12">
    <div class="col-sm-12 col-md-6 col-lg-6">
        <input type="text" class="form-control" formControlName="crew_name">
    </div>
    <div class="col-sm-12 col-md-6 col-lg-5">
        <!-- <input type="text" class="form-control" value="Writer"> -->
        <select class="form-control" type="text" formControlName="crew_role">
          <option [ngValue]="">Select</option>
          <option [ngValue]="1">Director</option>
          <option [ngValue]="2">Music Composer</option>
          <option [ngValue]="3">Writer</option>
        </select>
    </div>
    <div class="col-sm-1 col-md-1 col-lg-1"><p class="add-more mb-3"><a href="javascript:void(0)" (click)="removeContact(i)"> - remove</a></p></div>
  </div>
</div>
</span>
<p class="add-more mb-3"><a href="javascript:void(0)" (click)="addContact()">+ add</a></p>
<div class="row">
    <div class="col-12 text-right">
        <button class="btn btn-primary" type="submit">Save & Next</button>
    </div>
</div>
</form>

How can I select the default value in the select boxes as above?

  • Try this: https://stackoverflow.com/questions/3518002/how-can-i-set-the-default-value-for-an-html-select-element – Milan Lakhani Mar 12 '20 at 10:09
  • 1
    please set crew_role as '' in this.form = this.fb.group({ crew_role: [''] }); – Savaj Patel Mar 12 '20 at 10:09
  • 1
    When you bind an HTML form control to the form you have created, it will take on the value from the `FormControl`. You're setting `null` in the `crew_role` `FormControl` rather than an actual value – Kurt Hamilton Mar 12 '20 at 10:09

2 Answers2

1

Your crew_role <select> isn't defaulting to the Select value because you're not setting the correct value when building the form.

In your HTML you have

<select class="form-control" type="text" formControlName="crew_role">
  <option value="">Select</option>
  <option value="1">Director</option>
  <option value="2">Music Composer</option>
  <option value="3">Writer</option>
</select>

The Select option has the value "". You are setting it to null when you build the form.

Instead of

crew_role: [null],

You should have

crew_role: this.fb.control(''),

DEMO: https://stackblitz.com/edit/angular-ozgh6g

Kurt Hamilton
  • 12,490
  • 1
  • 24
  • 40
1
<select class="form-control" type="text" formControlName="crew_role">
          <option [value]="null">Select</option>
          <option [value]="1">Director</option>
          <option [value]="2">Music Composer</option>
          <option [value]="3">Writer</option>
        </select>

this.form = this.fb.group({
    crew_name: [null],
    crew_role: [null],
    contacts: this.fb.array([this.createContact()]),
  });

just use [value]="null"

Ar123
  • 73
  • 6