0

Angular 2 select tag with formControlName not working. pmsList having data like as follows: [{id:1,pms:'test'},.......] but when in constructor I set:

this.pms.setValue({id:1,pms:"test"})

then its doesn't reflects the value on html page.

here is my html code:

<select formControlName="pms" class="form-control down-arrow">
  <option value="">Select</option>
  <option *ngFor="let item of pmsList">{{item.pms}}</option>
</select>
3
  • Are you seeing any errors in the console? Commented Jan 18, 2018 at 14:38
  • Submit the code you are creating a form with. It could be many things. Commented Jan 18, 2018 at 15:42
  • what did you call you form ?? can you post your TS file ? Commented Jan 18, 2018 at 17:01

4 Answers 4

1

Make sure you are doing all of this:

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

public form: FormGroup;

constructor(private fb: FormBuilder) { 
    this.form = fb.group({
      pms: 'test'
    });
}

<form [formGroup]="form">
    <select formControlName="pms" class="form-control down-arrow">
        <option value="">Select</option>
        <option *ngFor="let item of pmsList">{{item.pms}}</option>
    </select>
</form>
Sign up to request clarification or add additional context in comments.

Comments

0

There is seomthing wrong no ? You start with this.pms, then update pms in setValue.

It should be something like that (aussming your form group is called myFormGroup):

this.myFormGroup.setValue({id:1,pms:"test"})

Comments

0

try this:
change myForm with your form's name

this.myForm.get('pms').patchValue({id:1,pms:"test"});

Comments

0

Recently using Angular 7 and material I experience the same problem, the solution for me was force return a string to the FormGroup

createCategoryForm(): FormGroup
{
    return this._formBuilder.group({
        id: [this.cat.id],
        name: [this.cat.name],
        type: [this.cat.type],
        parent_id: ["" + this.cat.parent_id]
    });
}

and then, just for testing, I have let fixed 2 options with ids 1 and 2

<div fxLayout="row" fxLayoutAlign="start start">
            <mat-form-field appearance="outline" fxFlex>
                <mat-label>Parent</mat-label>
                <mat-select name="parent_id" formControlName="parent_id">
                    <mat-option value="1">1</mat-option>
                    <mat-option value="2">2</mat-option>
                </mat-select>
            </mat-form-field>
        </div>

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.