1

I have a problem with setting a default value of select tag. Here is my some snippet:

<select class="form-control" id="field_company" name="company" formControlName="company">
    <option value="null" [selected]="true"> {{ 'khanbankCpmsApp.company.default' | translate }}</option>
    <option
        [ngValue]="companyOption.id === editForm.get('company').value?.id ? editForm.get('company').value : companyOption"
                *ngFor="let companyOption of companies;"
    >
       {{ companyOption.name }}</option>
</select>

When it's default by that static value should be displayed on select. But result is:

enter image description here

What did i do wrong ? any advice ?

enter image description here

2
  • Can you see what's the resulting value, by inspecting the elements via the developer tools? Commented Jun 10, 2019 at 12:01
  • @johey yes i will edit my question Commented Jun 10, 2019 at 12:03

2 Answers 2

1

You are using a reactive form, so you need to set the FormControl into your formGroup like.

<form [formGroup]="companyGroup">
    <select class="form-control"  name="company" formControlName="company">
        <option value="1"> One </option>
        <option value="2"> Two </option>
        <option value="3"> Three </option>
    </select>
</form>

and .ts file contains the

export class HelloComponent implements OnInit {
  @Input() name: string;
  companyGroup: FormGroup;

  constructor(private fb: FormBuilder) {

  }

  ngOnInit() {
    this.companyGroup = this.fb.group({
      company : ''
    })
    this.companyGroup.get('company').patchValue('2');
  }
}

Demo link Here

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

1 Comment

what, this is exactly same as my answer
1

Since you are using a reactive form, you should set the default value in the reactive form so that the correct option gets selected by the framework.

editForm = this.fb.group({ 
  name: [null, [Validators.required]], 
  status: [null, [Validators.required]], 
  dueDate: [], loanAmount: [], grantsOpenUntil: [], 
  users: [], 
  company: ['khanbankCpmsApp.company.default'], 
  });
<select class="form-control" id="field_company" name="company" formControlName="company">
    <option [ngValue]="'khanbankCpmsApp.company.default'"> {{ 'khanbankCpmsApp.company.default' | translate }}</option>
    <option
        [ngValue]="companyOption.id === editForm.get('company').value?.id ? editForm.get('company').value : companyOption"
                *ngFor="let companyOption of companies;"
    >
       {{ companyOption.name }}</option>
</select>

4 Comments

where that company gets from ?
company is your formControlName.. formControlName="company". If you use formbuilder it must be something like. fb.group({ company: [null, ...] })
editForm = this.fb.group({ name: [null, [Validators.required]], status: [null, [Validators.required]], dueDate: [], loanAmount: [], grantsOpenUntil: [], users: [], company: [null], });
Okay, try new suggestion.

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.