Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Incorrect binding type of the reactive control

I have a strange problem with one of my reactive control function (below). It seems to be bound to a type UserFunction instead of just string as initialized in *ngFor section of my template.
If I execute this.form.get('function').value it returns UserFunction object – I would expect the string?

UserFunction definition:

export class UserFunction {
    id: string;
    userFunction: string;
}

Component:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

userFunctions: UserFunction[] = [];
this.form = this.formBuilder.group({
                scheduledDate: ['', Validators.required],
                function: ['', [Validators.required, this.functionValidator]],
                required: [''],
                userAvailability: [''],
              });
              
        

HTML template:

    <select formControlName="function" class="form-control" [ngClass]="{ 'is-invalid': submitted && f.function.errors }">

               
                <option *ngFor="let item of userFunctions" [value]='item'>
                    {{ item.userFunction }}
                </option>
            </select>
        

Sorry I can’t explain that simpler then that.

>Solution :

You need to bind the value for options correctly. Change your HTML as follows,

        <select formControlName="function" class="form-control" [ngClass]="{ 'is-invalid': submitted && f.function.errors }">
            <option *ngFor="let item of userFunctions" [value]='item.userFunction'>
                {{ item.userFunction }}
            </option>
        </select>

Nothe the value is now

[value]='item.userFunction'
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading