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?

3
  • Try this: stackoverflow.com/questions/3518002/… Commented Mar 12, 2020 at 10:09
  • 1
    please set crew_role as '' in this.form = this.fb.group({ crew_role: [''] }); Commented Mar 12, 2020 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 Commented Mar 12, 2020 at 10:09

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks! I have just did some amendments in the question. instead value="1" , I did [ngValue]="1" [ngValue]="2" etc. Can you modify your answer based on this?
Why would you use ngValue instead of value when you just have simple keys?
No prob :) Just so you know, ngValue and value must be treated with a bit of care. I did an in-depth answer on it here: stackoverflow.com/a/60626545/5367916
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"

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.