2

html

 <select class="form-control" id="venue" name="venue1"
 formGroupName="venue" 
  (change)="venue_method($event.target.value)">
    <option hidden >Click to choose a venue...</option>
    <option [attr.value]="venue.id" *ngFor="let venue of venues"> 
       {{venue.name}}
     </option>
    <input type="hidden" formControlName="title" name="title">
  </select>

Component Code

this.rForm = fb.group({
    'venue': fb.group({
        'VenueId': [],
        'title': [],
        'isAvailable': [],
        //some more values
    }),
//some other controls and groups

});

venue_method(input: any) {
 console.log(input)
this.rForm.get('venue').patchValue({ title: input.name });
this.rForm.get('venue').patchValue({ VenueId: input.id });
this.rForm.get('venue').patchValue({isAvailable: true });
//some more patches
console.log(this.rForm.get('venue').value);
}

I want to send the value of all the properties my venue has, eg 'name'& 'id' but with attr value I can only patch one value, whats the proper workaround for this?

Note: I don't want select to enhance into showing all the option, I want to access all the properties of "venue" from "let venue of venues". venue has a venue.name and venue.id , currently i'm able to either access venue.id or venue.name from my "venue_method". how to access both at the same time?

1

1 Answer 1

2

You need to update your code a little bit as shared below. update your
<option [attr.value]="venue.id" *ngFor="let venue of venues"> to

<option value="venue | json" *ngFor="let venue of venues"> 

 <select class="form-control" id="venue" name="venue1"
 formGroupName="venue" 
  (change)="venue_method($event.target.value)">
    <option hidden >Click to choose a venue...</option>
    <option value="venue | json" *ngFor="let venue of venues"> 
       {{venue.name}}
     </option>
    <input type="hidden" formControlName="title" name="title">
  </select>

In the TS you need to parse your data to make object again.

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

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.