1

I have a problem with angular forms. I am trying to create a form that uses data from a mongo collection and provides it via the <select> directive. I have a FormBuilder that is initialized like this:

ngOnInit() {
    this.addForm = this.formBuilder.group({
        department_pk: ['', Validators.required],
        name_c: ['', Validators.required],
        location_fk: [],
        expirationdate_d: [],
    });
}

The Problem is the location_fk property. I am trying to fill the value of this property via the following <select> statement (I left the other form groups (for name_c, ...) out of this code snippet):

<form [formGroup]="addForm">                 
    <div class="form-group">
        <label>Location (*): </label>
        <select class="form-control" formControlName="location_fk" (ngModelChange)="onChange($event)" name="select">
              <option [ngValue]="i" *ngFor="let i of locations">{{i.name_c}}</option>
        </select>
    </div>
</form>

I managed to extract the value of i, however, I want to extract a property, i.location_pk, from the collection, while still displaying i.name_c in the selection that is visible for the user. Is there any way to do this?

1 Answer 1

1

Did you tried this?

<option [ngValue]="i['propName']" *ngFor="let i of locations">{{i.name_c}}</option>

or

<option [ngValue]="i.propName" *ngFor="let i of locations">{{i.name_c}}</option>
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, that did the trick! thought that i tried that already, kinda confused that didn´t work :P

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.