1

I am trying to pass an object as parameter using Angular 9.0.7 in a select.

Code looks like this:

<div *ngIf="foundAddresses == true">
    <label class="bold">Select Address</label>
    <select [ngClass]="{'error-message' : orderForm.controls['address'].touched && orderForm.controls['address'].hasError('required')}" (change)="updateAddress($event.target.value)" formControlName="address" class="titlewidth bottomspacing findAddressSelect">
        <option>Please select</option>
        <option *ngFor="let address of addresses" value="{{address}}">
            {{address.summaryline}}
        </option>
    </select>
</div>

The addresses is an array of objects, where address is an object containing different key-value pairs. An example of an object entry in the array looks like this:

    {
        "addressline1": "1 Heol Dolwen",
        "summaryline": "1 Heol Dolwen, Cardiff, South Glamorgan, CF14 1RX",
        "number": "1",
        "premise": "1",
        "street": "Heol Dolwen",
        "posttown": "Cardiff",
        "county": "South Glamorgan",
        "postcode": "CF14 1RX",
        "recodes": "CF4 1RX:1A:199812"
    },

And I am trying to get the value like this:

  updateAddress(address){
    
    this.addressSelected = true;
    this.selectedAddress = address;

    console.log(this.selectedAddress);
  }

When I am printing the value that gets passed over, the console log shows [object Object]. I have found some conflicting answers where people were using ngModel, however, I have noticed this method is deprecated.

The form has been created using this:

<form [formGroup]="orderForm" (ngSubmit)="onSubmit()" novalidate>

I have used formGroup as the variable type and formBuilder.group to add Validators to each field.

What should I change to be able to pass it over to my controller (.ts) file?

Sebastian
  • 87
  • 2
  • 11

1 Answers1

0

Just to confirm, you want to pass the entire address object to the updateAddress method on change?

Or if you just want the address.summaryline then below should work fine `

<option *ngFor="let address of addresses" value="{{address.summaryline}}">
            {{address.summaryline}}
        </option>

`

Anna
  • 259
  • 1
  • 6
  • I want to pass the entire address object. I want to use this as I need to update multiple values from the results of the object. The case you have mentioned above would work, of course, as the summaryline is only a string, not an Object. – Sebastian Mar 03 '22 at 13:06
  • In that case if I'm not wrong you can pass the unique key such as id to the value attribute and extract the address object matching to this id from the addresses array inside ts inside updateAddress(address){} method. this can be a workaround though, but as per my understanding, value attribute allow you to pass only a string and not an object. – Anna Mar 03 '22 at 13:27
  • That's the workaround that I've been doing now, just passing the index of the *ngFor and then getting the object from the array using the index. I was just wondering about a proper method of passing the object value as I am sure I might encounter a situation in which I won't be able to extract the object value with an index per say – Sebastian Mar 03 '22 at 13:30